PHP operators are a set of symbols that perform mathematical operations and other types of operations such as assignments, comparisons, string operations, etc.
These symbols are what we use to tell the PHP interpreter how to perform a calculation or processing on the data that we are working with.
There are 6 basic types of PHP operators. They are:
The PHP operators which fall in the category of arithmetic operators are explained by example below:
<?php echo 8 + 4; // performs addition echo 8 - 4; // performs subtraction echo 8 * 4; // performs multiplication echo 8 / 4; // performs division echo 8 % 5; // gives the modulus (remainder after division) ?>
The results of the above calculations (on separate lines) are:
As we just saw above, PHP operators were used to perform basic mathematical functions.
The basic assignment operation in PHP uses the equal sign ( = ) and is shown below:
<?php $someVariable = "anything"; $a = 3; $b = 4; $sum = $a + $b; // $sum now contains the value 7 ?>
By using the PHP assignment operator we are able to store (or assign) a value to a variable.
The comparison operators in PHP are shown below:
|==||equal to||true if a equals b, otherwise false|
|!=||not equal to||true if a does not equal b, otherwise false|
|===||identical to||true only if a equals b and they are of the same type|
|!==||not identical to||true only if a does not equal b or they are not of the same type|
|<||less than||true if a is less than b, otherwise false|
|>||greater than||true if a is greater than b, otherwise false|
|<=||less than or equal to||true if a is less than or equal to b, otherwise false|
|>=||greater than or equal to||true if a is greater than or equal to b, otherwise false|
If we were to output the result of a comparison operation directly we would get a 1 for true and no output for false.
The following comparisons will all return 1 because they are all true:
<?php $a = 3; $b = 3; $c = 10; // all of the following are true echo ($a == $b); echo ($a != $c); echo (8 === 8); echo ("8" !== 8); echo ($b < $c); echo ($b <= $b); echo ($c > $b); echo ($c >= $c); ?>
The PHP comparison operators allow us to test a particular condition and will return a boolean result. We can then use this boolean result as part of a PHP If Statement for example.
The following PHP operators increment (increase by 1) and decrement (decrease by 1) the value of a variable.
Look at the example below:
<?php $a = 5; $a++; echo $a; echo '<br />'; $a--; echo $a; ?>
As we just saw, the increment/decrement operators are used to increase or decrease the value of a numeric variable by 1.
PHP has a number of logical operators as shown below. These operators perform logical comparisons on variables and return Boolean values as results. The logical operators in PHP are:
|&&||and||a && b||true if a and b are true, otherwise false|
|and||and||a and b||true if a and b are true, otherwise false|
|||||or||a || b||true if a or b are true, otherwise false|
|or||or||a or b||true if a or b are true, otherwise false|
|xor||xor||a xor b||true only if a or b (but not both) are true|
|!||not||!a||true if a is false; false if a is true|
The PHP logical operators are used just like the PHP comparison operators as they return Boolean values as their result.
This operator is simply a period ( . ) and is used to concatenate (join) strings. Look at this example:
<?php echo "Hello " . "there"; ?>
Below is a table of PHP operator precedences. This list goes from highest precedence to lowest. Operators on the same line have a similar precedence. Remember that an operator with a higher precedence will always be evaluated before one with a lower precedence.
Of course, operations which are placed in brackets automatically have the highest precedence.
|(int) (float) (string) (array) (object) (bool)||casting|
|!||logical "not" (inverse)|
|* / %||arithmetic operator|
|+ - .||arithmetic and string|
|< <= > >= <>||comparison|
|== != === !==||comparison|
|= += -= *= /= .= %=||assignment|
The above table is very important to know. Note that you might get incorrect results when you do calculations if you do not take note of the precedence of the operators you are using. Remember that you can use regular brackets () to enforce a particular precedence.
We hope this tutorial on PHP operators has been helpful.