You can find logical, arithmetic, or comparison operators all around you. If you learned any other programming language or paid attention to a few mathematics classes, you probably all ready are familiar with them. We use logical operators in PHP to execute operations on variables and/or values.
Operators are mainly divided using the following criteria. Some of them have a few special cases that we will explain on the way.
The PHP arithmetic operators are mostly self-explaining. They normally are used to perform mathematical operations between numbers, such as addition, subtraction, multiplication etc.
Operator | Name | Usage | Explenation |
---|---|---|---|
+ | Addition | $x + $y | Sum of $y added to $x |
- | Subtraction | $x - $y | Difference of $y subtracted from $x |
* | Multiplication | $x * $y | Product of $x multiplied by $y |
/ | Division | $x / $y | Quotient of $x divided by $y |
% | Modulo | $x % $y | Remainder of $a divided by $b. |
** | Exponentiation | $x ** $y | Elevation of $x to the $y'th power. |
The PHP assignment operators are used to write a value into a variable. You may already know the "=" assignment operator. This is the simplest one and it means that the value from the right is assigned to the variable on the left.
, On the other hand, we will include here some operation that combines assignment and the above arithmetical operation. This is just shorthand form of writing arithmetical operation and assigning the value to a variable.
Operator | Name | Explanation | Usage | Equivalent |
---|---|---|---|---|
= | Equal | Basic value assignation | $x = $y | |
+= | Plus Equal | Addition operation | $x += y | $x = $x + $y |
-= | Minus Equal | Subtraction operation | $x -= $y | $x = $x - $y |
*= | Multiply Equal | Multiplication operation | $x *= $y | $x = $x * $y |
/= | Divide Equal | Division operation | $x /= $y | $x = $x / $y |
%= | Modulo Equal | Modulus operation | $x %= $y | $x = $x % $y |
.= | Concatenate Equal | Concatenate operation | $x .= $y | $x = $x . $y |
There are another assignment operators that are used to increase or decrees the value of a variable by 1
The $i++ and $i-- respectively are equivalent (or shorthand) operations of the following:
php$i = $j = 1;
$i++; // is the same as $i += 1; and $i = $i + 1;
echo $2; // prints 1
$j--; // is the same as $j -= 1; and $j = $j - 1;
echo $j; // prints 0
There is also another way to perform the same operation
php$i = $j = 1;
++$i;
echo $i; // prints 2
--$j;
echo $j; // prints 0
Although many times, this operation result is the same, there are few minor logical differences. Try directly printing the $i++ value. You will be surprised.
php$a = $b = $c = $d = 5;
echo $a++; // prints 5
echo ++$b; // prints 6
echo $c--; // prints 5
echo --$d; // prints 4
I am sure you already notice the difference and know the logic behind it
Comparison operators are mostly used in if statements and conditional expressions in general. Comparison operations always evaluate to either true or false.
Assume: $x = 2 and $y = 3;Operator | Name | Usage | Evaluate to... |
---|---|---|---|
== | Equal | $x == $y | Returns true if $x is equal to $y |
=== | Identical | $x === $y | Returns true if $x is equal to $y, and they are of the same type |
!= | Not equal | $x != $y | Returns true if $x is not equal to $y |
<> | Not equal | $x <> $y | Returns true if $x is not equal to $y |
!== | Not identical | $x !== $y | Returns true if $x is not equal to $y, or they are not of the same type |
> | Greater than | $x > $y | Returns true if $x is greater than $y |
< | Less than | $x < $y | Returns true if $x is less than $y |
>= | Greater than or equal to | $x >= $y | Returns true if $x is greater than or equal to $y |
<= | Less than or equal to | $x <= $y | Returns true if $x is less than or equal to $y |
As you already guessed, comparison operators, allow you to compare two or more values of various conditional statements
Operator | Name | Usage | Result |
---|---|---|---|
and | And | $x and $y | True if both $x and $y are true |
or | Or | $x or $y | True if either $x or $y is true |
xor | Xor | $x xor $y | True if either $x or $y is true, but not both |
&& | And | $x && $y | True if both $x and $y are true |
|| | Or | $x || $y | True if either $x or $y is true |
! | Not | !$x | True if $x is not true |
The && and || logical operators seem to have more general recognition and background usage, although the and and or form of the same logical operators is taking more and more acceptation for the sake of readability.
phpfalse and false => false
false and true => false
true and false => false
true and true => true
phpfalse or false => false
false or true => true
true or false => true
true or true => true
php!false => true
!true => false
You may find this normally as array operators, but they are same good all logical operators applied to arrays. Arrays are basically a type of variables that can store multiple values.
Operator | Name | Usage | Result |
---|---|---|---|
+ | Union | $x + $y | Union of $x and $y |
== | Equality | $x == $y | Returns true if $x and $y have the same key/value pairs |
=== | Identity | $x === $y | Returns true if $x and $y have the same key/value pairs in the same order and of the same types |
!= | Inequality | $x != $y | Returns true if $x is not equal to $y |
<> | Inequality | $x <> $y | Returns true if $x is not equal to $y |
!== | Non-identity | $x !== $y | Returns true if $x is not identical to $y |
Instanceof is used to determine whether a PHP variable is an instantiated object of a certain class. instanceof can also be used to determine whether a variable is an instantiated object of a class that inherits from a parent class, but this is not the subject of this tutorial. You can read more about this on the php.net manual site.