Home  About  Downloads  Features  Demo  FAQ  Forum  Screenshots  PHP Tutorials  Hire a PHP Programmer  
<< PHP Strings 
PHP Tutorials 
PHP If...Else >>
PHP OperatorsYou've seen this throughout the whole tutorial, and now I finally explain it? Not very smart on my part. Anyways, yes, the assignment operator is very very very important in PHP. You couldn't really do anything without it. In case you're wondering, the assignment operator is = and can be used like so:
<?php $variable = 2; $another_one = false; ?> That will assign the value of 2 to the variable $variable and the value of false to the variable $another_one. Mathematical OperatorsYeah isn't that just great, math. Actually it is, and a lot of programming involves mathematical equations! Anyway the mathematical operators in PHP are basically the same as those in the real world:
Remember that the remainder $x % $y is negative for negative $x. Isn't that nice? PHP will do all that hard math for you :P Parentheses (brackets) can also be used, as shown in the following example:
<?php $addition = 1 + 2; $substraction = 2  1; $multiplication = 2 * 2; $division = 4 / 2; $modulus = 11 % 2; $complex = ((5 * 2) / (2 * 1))  10 + (2 * 2); echo "1 plus 2 equals {$addition}<br />"; echo "2 minus 1 equals {$substraction}<br />"; echo "2 times 2 equals {$multiplication}<br />"; echo "4 divided by 2 equals {$division}<br />"; echo "Remainder of 11 divided by 2 equals {$modulus}<br />"; echo "Negative 5 times 2 divided by 2 times 1 minus 10 plus 2 times 2 equals {$complex}<br />"; ?> What the end users will see:
1 plus 2 equals 3 2 minus 1 equals 1 2 times 2 equals 4 4 divided by 2 equals 2 Remainder of 11 divided by 2 equals 1 Negative 5 times 2 divided by 2 times 1 minus 10 plus 2 times 2 equals 11 Math in PHP follows the same precedence rules as normal math does. Sometimes people are taught the B.E.D.M.A.S. (or P.E.D.M.A.S.) way of remembering it. If you weren't taught either of these ways, they mean the order Parentheses/Brackets, Exponents, Division, Multiplication, Addition and Substraction. All math in PHP will be done in that order. Mathematical Comparison in PHPComparison is very important in PHP. If you need to find out whether two things are equal, not equal, bigger or smaller than one another, PHP has built in functionality for this. For the next example, let's say $x = '1' and $y = 1:
* Less common, use != ShorthandThere are shorthand ways of writing operators in PHP to. You can combine the assignment operator with a mathematical operator like these:
There is also a way to have a shorthand for connecting strings.
There is even a shorter way to add 1 or substract 1 from a variable. These two methods are called incrementing and decrementing. Here is an example:
<?php $x = 1; echo "x is {$x}<br />"; $x++; echo "x is {$x}<br />"; $x; echo "x is {$x}<br />"; ?> This is what the end users will see:
x is 1 x is 2 x is 1 As you can see from the code above, the syntax for this is $variable++ and $variable. There is also another method that looks like ++$variable that and $variable that. They are different than the ones above as they will add one (or substract one) from the variable before the line is done executing. You can see this in this example:
<?php $x = 1; echo "x is {$x}<br />"; echo 'x is ' . $x++ . '<br />'; echo "x is {$x}<br />"; $x = 1; echo "x is {$x} (set again)<br />"; echo 'x is ' . ++$x . '<br />'; echo "x is {$x}"; ?> What the end users will see:
x is 1 x is 1 x is 2 x is 1 (set again) x is 2 x is 2 See the difference? << PHP Strings  PHP Tutorials  PHP If...Else >> 

© Douglas Rennehan 2007  2017  Terms of Service  Privacy Policy  Valid XHTML 1.0  Valid CSS 