PHP Operators Tutorial
In this tutorial we learn more about symbols that act as operators in PHP and allow us to perform various operations throughout our application.
These operations include arithmetic, assignment, compound assignment, comparison, logical and more.
What are operators?
Operators are symbols that have special meaning in PHP, and they indicate the types of operations that can be done.
As an example, we can consider the + symbol as an operator. It adds whatever number is on the right to the number on the left. This is an operation that is taking place on two operands.
PHP categorizes operators into different types:
- Array operators
- Arithmetic operators
- Assignment operators
- Conditional assignment operators
- Comparison operators
- Incremental operators
- Logical operators
- String operators
Array operators
Array operators are used to compare arrays. The array operators below may not make any sense at this point, if you are a beginner. We will cover these operators again in the tutorial lesson on arrays.
The following table lists array operators in PHP.
Operator | Name | Description |
---|---|---|
+ | Union | The union of two arrays |
== | Equality | Returns true if both arrays have the same value pairs. |
=== | Identity | Returns true if both arrays have the same value pairs in the same order and of the same types |
!= | Inequality | Returns true if both arrays do not have the same value pairs |
<> | Inequality | Returns true if both arrays do not have the same value pairs |
!== | Non-identity | Returns true if both arrays do not have the same value pairs in the same order and of the same types |
Arithmetic operators
Arithmetic operators are used with numerical values to perform common mathematical arithmetic.
The following table lists arithmetic operators in PHP.
Operator | Name | Description |
---|---|---|
+ | Addition | Adds the right operand to the left operand |
- | Subtraction | Subtracts the right operand from the left operand |
* | Multiplication | Multiplies the left operand with the right operand |
/ | Division | Divide the left operand by the right operand |
% | Modulus | Remainder of the division of the left operand by the right operand |
** | Exponent | Raise the left operand to the power of the right operand |
<?php
$br = "<br>";
$a = 3;
$b = 4;
// Addition
echo $a + $b;
echo $br;
// Subtraction
echo $b - $a;
echo $br;
// Multiplicaion
echo $a * $b;
echo $br;
// Division
echo $b / $a;
echo $br;
// Modulus
echo $b % $a;
echo $br;
// Exponent
echo $b ** $a;
echo $br;
?>
Assignment operators
Assignment operators are used with numerical values to assign a value to a variable.
The following table lists assignment operators in PHP.
Assignment | Same as | Description |
---|---|---|
= | a = b | The left operand is assigned the value of the operand or expression on the right |
+= | a = a + b | The left operand is assigned the value of itself added to the right operand |
-= | a = a - b | The left operand is assigned the value of the right operand subtracted from itself |
*= | a = a * b | The left operand is assigned the value of itself multiplied by the right operand |
/= | a = a / b | The left operand is assigned the value of itself divided by the right operand |
%= | a = a % b | The left operand is assigned the value of the remainder of division of itself divided by the right operand |
<?php
// Assignment
$br = "<br>";
$a = 3;
// Addition Assignment
echo $a += 10;
echo $br;
// Subtraction Assignment
echo $a -= 2;
echo $br;
// Multiplicaion Assignment
echo $a *= 5;
echo $br;
// Division Assignment
echo $a /= 1;
echo $br;
// Modulus Assignment
echo $a %= 1;
echo $br;
?>
Conditional assignment operators
Conditional assignment operators will set values depending on the outcome of certain conditions. The conditional operators below may not make any sense at this point, if you are a beginner. We will cover them again in the tutorial lesson on conditional control flow.
The following table lists conditional assignment operators in PHP.
Operator | Name | Example | Description |
---|---|---|---|
? : | Ternary | $a = expression ? expT : expF | If the expression is true, $a will be the value of expT |
If the expression is false, $a will be the value of expF | |||
? ? | Null coalescing | $a = exp1 ?? exp2 | If exp1 exists and is not NULL, $a will be the value of exp1 |
If exp1 does not exist or its value is null, $a will be the value of exp2 |
The null coalescing operator is not available in versions below PHP v7.
Comparison operators
Comparison operators will compare two values against each other and return a true or false boolean value.
The following table lists comparison operators in PHP.
Operator | Name | Description |
---|---|---|
== | Equal | Returns true if the right operand is the same as the left operand |
=== | Identical | Returns true if the right operand is the same as the left operand and both are of the same type |
!= | Not equal | Returns true if the right operand is not the same as the left operand |
<> | Not equal | Same as above |
!== | Not identical | Returns true if the right operand is not the same as the left operand or if they are both not of the same type |
> | Greater than | Returns true if the left operand is greater than the right operand |
< | Less than | Returns true if the left operand is less than the right operand |
>= | Greater than or equal to | Returns true if the left operand is greater than or equal to the right operand |
<= | Less than or equal to | Returns true if the left operand is less than or equal to the right operand |
It is important for programmers coming from Javascript to remember that three equal signs === represents evaluating both equal value and equal type, and not just equal value.
<?php
$br = "<br>";
// Equal
echo 5 == 5;
echo $br;
// Identical
echo "A" === "A";
echo $br;
// Not equal
echo 3 != 4;
echo $br;
// Not identical
echo 3 !== "Three";
echo $br;
// Greater than
echo 100 > 99;
echo $br;
// Less than
echo 30 < 40;
echo $br;
// Greater than or equal to
echo 2 >= 2;
echo $br;
// Less than or equal to
echo 6 <= 6;
echo $br;
?>
Incremental operators
Incremental operators are used to increase a value incrementally, and decremental operators are used to decrease a value. These operators are what is known as unary operators and as such only require one operand.
The following table lists incremental and decremental operators in PHP.
Operator | Name | Description |
---|---|---|
++$a | Pre-increment | Increment by one, then return $a |
$a++ | Post-increment | Return $a, then increment by one |
--$a | Pre-decrement | Decrement by one, then return $a |
$a-- | Post-decrement | Return $a, then decrement by one |
<?php
$br = "<br>";
$a = 1;
echo $a;
echo $br;
// Increment and return value
echo ++$a;
echo $br;
// Return value, then increment
echo $a++;
echo $br;
echo $a;
echo $br;
// Decrement and return value
echo --$a;
echo $br;
// Return the value, then decrement
echo $a--;
echo $br;
echo $a;
?>
In the example above we can see that in both pre and post increments the value is incremented. The choice of which to use is determined by which value we want to work with:
- If we want to work with the already incremented value, we choose the pre-increment.
- If we want to work with the value before it is incremented and increment it afterwards, we choose the post-increment.
In most situations it won’t matter because we only want the value to actually increment, and not specifically use the value. We will look into increments and decrements more in the tutorial lesson on looping control flow.
Logical operators
Logical operators are used to combine conditional evaluations. We will work with them again in the tutorial lesson on conditional control flow.
The following table lists logical operators in PHP.
Operator | Name | Description |
---|---|---|
and | And | Returns true if one expression and another is true |
or | Or | Returns true if one expression or another is true, or both expressions are true |
xor | Xor | Returns true if one expression or another is true, but not both |
&& | And | Returns true if one expression and another is true |
|| | Or | Returns true if one expression or another is true, or both expressions are true |
! | Not | Returns true if the expression immediately following is not true |
<?php
$br = "<br>";
// And
echo 1 == 1 and 2 == 2;
echo $br;
// Or
echo 1 == 1 or 2 == 2;
echo $br;
// Xor
echo 1 == 2 or 2 == 2;
echo $br;
// && (and)
echo 1 == 1 and 2 == 2;
echo $br;
// || (or)
echo 1 == 1 or 2 == 2;
echo $br;
// ! (not)
echo !false;
?>
String operators
String operators are operators that combine (concatenate) strings together.
The following table lists string operators in PHP.
Operator | Name | Description |
---|---|---|
. | Concatenation | Combine one string with another |
.= | Concatenate and assign | Append one string to another |
<?php
$br = "<br>";
$str1 = "Hello";
$str2 = "there";
// Concatenation
echo $str1 . " " . $str2;
echo $br;
// Append
echo $str1 .= $str2;
?>
Summary: Points to remember
- Operators are like keywords that have special meaning in PHP, and indicate operations that can be done.
- The null coalescing operator ? ? is not available in versions below PHP 7.
- Javascript programmers should note that === means evaluating both equal value and equal type, and not only equal value.
- These operators will be used more throughout the tutorial series, and will become much clearer for you.