<< PHP Strings | PHP Tutorials | PHP If...Else >>

PHP Operators

You'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 Operators

Yeah 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:

  • -$x : Negation : Returns the opposite of $x
  • $x + $y : Addition : Returns the sum of $x and $y
  • $x - $y : Substraction: Returns the difference of $x and $y
  • $x * $y : Multiplication: Returns the product of $x and $y
  • $x / $y : Division : Returns the quotient of $x and $y
  • $x % $y : Modulus : Returns the remainder of $x divided by $y

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 PHP

Comparison 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:

  • $x == $y : Equals : true
  • $x === $y : Identical : false
  • $x != $y : Not Equal : false
  • *$x <> $y : Not Equal : false
  • $x !== $y : Not Identical : true
  • $x < $y : Less Than : false
  • $x > $y : Greater Than : false
  • $x <= $y : Less Than or Equal To : true
  • $x >= $y : Greater Than or Equal To : true

* Less common, use !=

There are other ways to use this functionality too, but it is in the If...Else section. You may notice that they are not identical. Identical in PHP means of the same type, in the example above one is a string and one is an integer. You may ask, 'Why are they equal then?'. Well this is because PHP will convert a string to an integer if you're comparing it with another integer.

Shorthand

There are shorthand ways of writing operators in PHP to. You can combine the assignment operator with a mathematical operator like these:

  • $x += 1 : Adds 1 to $x
  • $x -= 1 : Substracts 1 from $x
  • $x /= 1 : Divides $x by 1
  • $x *= 1 : Multiplies $x and 1
  • $x %= 1 : Remainder of $x divided by 1

There is also a way to have a shorthand for connecting strings.

  • $x .= 'yup' : Adds yup to the end of $x

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 >>