PHP operators
OK, so we have values (like 1, 5, "bob") and we have variables (like $a, $bob) that can store the values. Wouldn't it be nice to be able to do stuff (operate) on those?
Smooth operator
If you look at this thing, what do you see
2 + 3
You see two numbers (values) and a plus sign (a operator). And here:
$a - $b
These are two variables and an operator.
An operator is something that works with values or variables and in return gives you another value.
What's the result of executing this code 2 + 3? The value 5.
And this code: $a - $b? The result is again a value, it's whatever the value contained in $a was, minus the value contained in $b. So if $a was 3 and $b was 2, the result of the - operator (also called subtraction operator) will be 1.
Arithmetic operators
What arithmetic operators exist in PHP? Let's check out some examples.
Assuming that
$a = 6; $b = 5;
echo $a + $b;will print 11echo $a - $b;will print 1echo $b - $a;will print -1echo -$a;will print -6echo $a * $b;will print 30echo $a / $b;will print 1.2echo $a % $b;will print 1. This is the modulus operator, it returns the remainder of the division. When you divide 6 / 5 the result is not a whole number (integer). But you can divide 5 / 5 and the result is an integer. Since 6 - 5 is 1, this is the remainder of the division. What would be 7 % 5? Hint: the result is 7 minus the biggest number (but smaller than 7) that returns an integer when divided by 5. The result is 2. Whats 7 % 2? 6 / 2 is an int, and 7 - 6 is 1, so 7 % 2 is 1. This could be useful if for example you want to check if a number is odd or even. By definition even numbers divide by 2 with no remainder. In our example above$a % 2is 0, so$ais even.$b % 2is not 0, so$bis odd.
Precedence
You can use several operators in one statement. Like:
echo $a - $b + 2; // will print 3
Combining several operators can sometimes be ambiguous. In the example above - is it $a minus $b first and then adding 2 to the result? Or $b + 2 first and then $a minus the result? In this specific case it doesn't matter, but in the next it does:
echo $b + $a * $b; // will print 35
6 * 5 is 30 and 30 + 5 is 35. This is because * has precedence over +. You can alter the precedence of the operators using brackets.
echo $b + ($a * $b); // will print 35, same as above
echo ($b + $a) * $b; // will print 55
There is a table with the precedence of the operators in the PHP manual, you can consult it when needed, but the rule of thumb is: just use brackets. Even when it's not really necessary. Don't overdo it, of course, but when someone is looking at your code (this "someone" person might be you 3 months down the road) it should be clear what the code does without even thinking about it or consulting a manual.
String concatenation
String concatenation was discussed already, but let's repeat once again that the dot operator is used to glue two strings together, like this:
$a = 'a'; $b = 'b'; $c = 'cde'; echo $a . $b . $c; // prints "abcde"
Increment operators
These are fun, it's when you put two plus signs or two minus signs together. In the case of two plus signs, the value of the variable they refer to is increased by one. Example:
$a = 6; // a is 6 $a++; // a is now 7 echo $a; // prints 7
Same for decrementing:
$a = 6; // a is 6 $a--; // a is now 5 echo $a; // prints 5
There is also ++$a which is different than $a++. ++$a is called pre-increment, meaning that it increments the value and returns the new value. $a++ is called post-increment, meaning that it returns the old value and then increments. In an example:
$a = 6; // a is 6 echo $a++; // prints the old value 6 echo $a; // prints 7, the new value $a = 6; // a is 6 echo ++$a; // prints the new value 7 echo $a; // prints 7
Same is for the decrement - $a-- is called post- and --$a is called pre-decrement.
In reality, post-increments and post-decrements are mostly used, so I'd say stay away from ++$a and --$a. If there's any ambiguity, just don't mix the increment/decrement with other operators or statements where it's not 100% clear at a first glance what's going on. Some people go as far as saying that the increment operators should be banned and replaced with the more readable (albeit longer to type) version:
$a = $a + 1; // instead of $a++ $a = $a - 1; // instead of $a--
Assignment operators
You know the simple assignment operator =, as in $a = 1;. There are others which are combination of an arithmetic (or concatenation) operator and an assignment. Check those out:
$a = 1; $a += 10; // $a is 11 (1 + 10) $a -= 1; // $a is 10 (11 - 1) $a *= 10; // $a is 100 (10 * 10) $a /= 2; // $a is 50 (100 / 2); $a %= 2; // $a is 0 $a = 'abc'; $a .= 'def'; // $a is "abcdef"
Other types of operators
In addition to the operators discussed above, there are also other kinds. Another breed of operators was mentioned in the article about variables. Those were the type casting operators, like:
$a = 1.2; echo (int)$a;
(int) is an operator, it takes a variable (or a value) and returns a value.
We'll see more operators in further articles, we'll learn them as we need them.