Operators
There are some operators that work directly on variables to save you time and effort in programming. For example, ++ and -- work to add and remove 1 from contents of the variable given:
PHP Code:
$foo = 3;
$foo = $foo + 1 // The 'old' way of doing this.
$foo++; // $foo is now 5
++$foo; // $foo is now 6
The difference between ++$foo and $foo++ is the point at which the number is added. If you're using $foo in a statement more complicated than those above, you'll find that :
PHP Code:
$foo = 3;
$bob = $foo++; // $bob is 3, $foo is 4.
$foo = ++$bob; // $bob and $foo are both 4
In the first line, we set $foo to 3. The following line reads in plain English as "Set $bob to the value of $foo, then add one to $foo", whereas the last line reads "Add one to $bob and set $foo to the new value". In general, if you have to think about what's going to happen, then you should write your code in a more simple way -- think of the poor git who's going to replace you! You're excused if:
PHP Code:
*
you're not coding for money
*
you won't be replaced
*
you remember why you do everything that you do and never have to check your own notes.
If you want to add/subtract more than one, use the += and -= operators. For strings you can use the .= operator.
PHP Code:
$foo = "Fred is ";
$foo .= " Bob's friend"; // $foo now contains "Fred is Bob's friend";
$foo = 1;
$bob = 3;
$foo += 2; // $foo is now 3
$foo += $bob; // $foo is now 6
The usual operators
Use + to add, - to subtract, / to divide, * to multiply, and % to get the modulus (remainder). These make sense with numbers. They don't with words. Use . to join multiple items (like pieces of text):
PHP Code:
echo "You have " . $items . " items.";
Comparison operators
Use comparison operators in tests like if, while and for loops to test a condition. These operators will compare the items on either side and will return true or false. Again, most of these only work meaningfully with numbers.
PHP Code:
$a == $b Returns true if the contents of $a match those of $b, false otherwise.
$a < $b True if $a is less than $b
$a > $b True if $a is greater than $b
$a <= $b True if $a is less than, or equal to $b
$a >= $b True if $a is greater than, or equal to $b
Logical operators
As above, these operators will return true or false depending on the outcome of the test it performs on the two sides of the operator. It takes each side to be a binary value, either true or false. In PHP, 0 and NULL are false, everything else (including negative numbers) is TRUE.
AND
Returns true if both sides of the operator evaluate to true. However, if the left hand side isn't TRUE, PHP won't bother testing the right hand side.
AND NOT
Returns true if the left side is TRUE and right side is FALSE.
OR
Returns true if the left side or the right side is true. Note that PHP won't bother checking the right hand side if the left hand side is true.
NOT
This is one-sided only. ! $a will be TRUE if $a is FALSE.
Because PHP (Like C and Perl) "short-circuits" these operators when it can, you can use them as control structures:
PHP Code:
($fp = fopen("filename.txt", 'r')) || die("Couldn't
open file for read");
PHP runs the first side, ($fp = fopen("filename.txt", 'r')). If the fopen() function call opens the file and the file pointer is stored in $fp, then this side of the line returns TRUE. || means "one side or the other (or both) must be true", PHP doesn't bother running the right hand side because it knows that one side is already true which is what the operator requires. Now, if the file-open function had failed, the left hand side would've returned FALSE so PHP would -=have=- to check the right hand side to see if that's true. The right hand side runs the die() function which quits PHP with an error message, in this case "Couldn't open file for read". In short, PHP quits if it can't open the file.
This tutorial was written by another one of my friends, if you would like to use this tutorial please send me a PM