Tuesday, June 26, 2012

PHP Lesson - Basic Programming (1)

After talking about basic php components, such as strings, intergers, arrays, let's move on to inergrate them together and start our basic programming journey.

First, we begin with setting up conditions. Put into simply words, Conditions are expressions that PHP tests or evaluates to see whether they are true or false. Some common operator often see including:
== Are the two values equal in value?
=== Are the two values equal in both value and data type?
> Is the first value larger than the second value?
>= Is the first value larger than or equal to the second value?
< Is the first value smaller than the second value?
<= Is the first value smaller than or equal to the second value?
!== Are the two values not equal to each other in either value or data type?

PHP tests comparisons by evaluating them and returning a Boolean value, either TRUE or FALSE. For example, look at the following comparison:
$a==$b If $a=1 and $b=1, the comparison returns TRUE. If $a =1 and $b =2, the comparison
returns FALSE.

Sometimes you just need to know whether a variable exists or what type of data is in the variable. Following are some common ways to test variables:
isset($varname) # True if variable is set, even if nothing is stored in it.
empty($varname) # True if value is 0 or is a string with no characters in it or is not set.
is_array($var2): Checks to see if $var2 is an array
is_float($number): Checks to see if $number is a floating point
number
is_null($var1): Checks to see if $var1 is equal to 0
is_numeric($string): Checks to see if $string is a numeric string
is_string($string): Checks to see if $string is a string

Sometimes you need to compare character strings to see whether they fit
certain characteristics, rather than to see whether they match exact values. For example, when we gather comments from visitors, we want to make sure they are offering a valid email address. The following are some commonly used special characters that you can use in patterns.
^ Beginning of the line
$ Ending of the line
. Any single character
? Preceding character is optional
() Groups literal characters into grema string that must be matched exactly
[] A set of optional characters
- All the characters between two characters
So if we want to validate a user's mail address, we can use: ^.+@.+\.com$
The expression ^.+@.+\.com$ matches the string: johndoe@somedomain.com


View the original article here

No comments:

Post a Comment