PHP operators - PHP arithmetic operators
Given two variables $var1 and $var2 in your script, you can use:
-
the addition operator:
$var1+$var2 -
the subtraction operator:
$var1-$var2 -
the multiplication operator:
$var1*$var2 -
the division operator:
$var1/$var2 -
the negation operator:
-$var1
PHP operators - PHP assignment operators
There are several assignment operators in PHP:
-
the basic PHP assignment operator =:
$a = 3 -
the combined operator +=:
$a += 3 (which is equivalent with $a = $a + 3) -
the combined operator .=:
$a .= 'hello' (which is equivalent with $a = $a.'hello';
Remark:
It is very important that you do not make any confusion between the equal sign == and the assignment operator =; indeed, when you want to test a variable for equality, you must use the equal sign ==, while the assignment operator = is used to define a PHP variable. Unfortunately, this can constitute a major source of errors for PHP beginners since in many other programming languages the assignment operator is :=, while the equal sign is the most standard =.
PHP operators - PHP comparison operators
We can distinguish the following PHP comparison operators:
-
the equal sign ==:
this is the sign we were talking about in the previous section. This PHP comparison operator tests for equality in a "loose way", meaning that the two expressions tested need not be of the same type: for instance, testing 2 == '2' will return TRUE, while in fact 2 is an integer whereas '2' is a string. Indeed, when you compare an integer with a numerical string, the numerical string will first be converted into an integer; similarly, if you compare two numerical strings, they will first be converted into integers.
In order to remedy this limitation, another symbol (the identical sign) was introduced. -
the identical sign ===:
the identical sign checks for rigorous equality (i.e. equality + equality of types). -
the not equal sign !=:
the not equal sign checks for absence of equality (in the sense ==). - the not identical sign !==: the not identical sign returns TRUE if the two elements being tested are not identical (in the sense ===).
-
the less than sign <:
$a < $b returns TRUE if $a is strictly less than $b. -
the sign greater than >:
$a > $b returns TRUE if $a is strictly greater than $b. -
the sign less than or equal to <=:
$a <= $b returns TRUE if $a is strictly less than or equal to $b. -
the sign greater than or equal to >=:
$a >= $b returns TRUE if $a is strictly greater than or equal to $b.
PHP operators - PHP logical operators
The logical operators in PHP are:
-
the and operator:
$a$ and $b$ returns TRUE when both $a$ and $b$ are TRUE
$a$ and $b$ can also be written $a && $b; the two expressions are almost equivalent, the only difference being that && has a higher precedence than most usual assignment and comparison operators; for instance, have a look at the PHP code below:
Learn the PHP code:
<?php
$a = true && false;
if ($a == false)
echo '$a is FALSE';
else
echo '$a is TRUE';
?>
Run the PHP script in your web browser:
Remark:
In the above PHP code, the expression $a = true && false is equivalent to the expression $a = (true && false) because the operator && has higher precedence than the operator =. Thus in this case the variable $a will be FALSE.
This is not the case for the operator and, so that the expression $a = true and false will be interpreted as ($a = true) and false. Thus in this case the variable $a will be TRUE. You can verify this by executing the modified PHP code. -
the or operator:
$a$ or $b$ returns TRUE when either $a$ or $b$ is TRUE
$a$ or $b$ can also be written $a || $b; here again, the two expressions are rouhgly equivalent, the only difference being that && has a higher precedence. -
the xor operator:
$a$ xor $b$ returns TRUE when either $a$ or $b$ is TRUE, but not both. -
the not operator, written !:
!$a returns TRUE when $a is FALSE (i.e. when $a is not TRUE).
PHP operators are very much like those you have probably already met in other programming languages; the only point that demands special attention is that the equal operator in PHP is written == and not = (which is the PHP assignment operator).
Next tutorial: PHP array and array of arrays
Previous tutorial: PHP conditional statements and loops
Back to computer forums
