In this article, you will learn about PHP Operators, different types of operators and how they can be used in PHP programs for different operations.
What are PHP Operators?
Operators are symbols that tell the PHP processor to perform certain actions. The expression 5 – 1 is equal to 4. Here 5 and 1 are called operands and – is called the operator. Operators are used to perform operations on variables and values.
PHP language supports the following type of operators.
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
Arithmetic Operators
The arithmetic operators are used for arithmetical operations, such as addition, subtraction, multiplication, etc. They take two operands which is why they are called binary operators. Here is the list of arithmetic operators in PHP.
Operator | Description | Example | Result |
+ | Addition | $a+$b | Sum of $a and $b |
– | Subtraction | $a-$b | Difference of $a and $b |
* | Multiplication | $a*$b | Product of $a and $b |
/ | Division | $a/$b | Quotient of $a and $b |
% | Modulus | $a%$b | Reminder of $a Divided by $b |
** | Exponentiation | $a**$b | Introduced in PHP 5.6. a raised to the power of b i.e a^b |
Example
<?php
$a = 5; // first variable
$b = 4; //second variable
echo($a + $b); //output will be 5 + 4 = 9
echo($a - $b); //output will be 5 - 4 = 1
echo($a * $b); //output will be 5 * 4 = 20
echo($a / $b); //output will be 5 / 4 = 1.25
echo($a % $b); //output will be 5**4 = 625
?>
PHP Assignment Operators
The PHP assignment operators are used to write a value to a variable or simply assigning a value to a variable. Here are the assignment PHP operators along with their descriptions.
Operator | Description | Example | Is Equivalent to |
= | Simple assignment operator | $a = $b | $a = $b |
+= | Add and assignment operator | $a += $b | $a = $a + $b |
-= | Subtract and assignment operaor | $a -= $b | $a = $a – $b |
*= | Multiply and assignment operator | $a *= $b | $a = $a * $b |
/= | Divide and quotient assignment operator | $a /= $b | $a = $a / $b |
%= | Divide and modulus assignment operator | $a %= $b | $a = $a % $b |
Example
<?php
$a = 5; //simply assigns the value 5 to variable a
echo $a; //output will be 5
$b = 5;
$b += 10; //add and assignment
echo $b; //output will be 15
$c = 10;
$c -= 5; //subtract and assignment
echo $c; //output will be 5
$d = 5;
$d *= 4; //multiply and assignment
echo $d; //output will be 20
$e = 5;
$e /= 2; //divide and quotient assignment
echo $e; //output wil be 2.5
$f = 5;
$f %= 2; //divide and modulus assignment
echo $f; //output will be 1
?>
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
Operator | Name | Example | Result |
== | Equal | $a == $b | True if $a is equal to $b |
=== | Identical | $a === $b | True if $a is equal to $b, and they are of the same type |
!= | Not equal | $a != $b | True if $a is not equal to $b |
<> | Not equal | $a <> $b | True if $a is not equal to $b |
!== | Not identical | $a !== $b | True if $a is not equal to $b, or they are not of the same type |
< | Less than | $a < $b | True if $a is less than $b |
> | Greater than | $a > $b | True if $a is greater than $b |
>= | Greater than or equal to | $a >= $b | True if $a is greater than or equal to $b |
<= | Less than or equal to | $a <= $b | True if $a is less than or equal to $b |
The following example will show you these comparison operators in action:
<?php
$a = 5;
$b = 15;
$z = "5";
var_dump($a == $z);
echo "<br>";
var_dump($a === $z);
echo "<br>";
var_dump($a != $b);
echo "<br>";
var_dump($a !== $z);
echo "<br>";
var_dump($a < $b);
echo "<br>";
var_dump($a > $b);
echo "<br>";
var_dump($a <= $b);
echo "<br>";
ar_dump($a >= $b);
?>
Output:
bool(true)
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable’s value and the PHP decrement operators are used to decrement a variable’s value. These are called unary operators as they require single operand to operate upon.
Operator | Name | Effect |
++$a | Pre-increment | $a = $a + 1, then returns $a |
$a++ | Post-increment | Returns $a, then performs $a = $a + 1 |
–$a | Pre-decrement | $a = $a – 1, then returns $a |
$a– | Post-decrement | Returns $a, then performs $a = $a – 1 |
The following example will show you these increment and decrement operators in action:
<?php
$a = 25;
echo ++$a; //outputs 26
$a = 25;
echo $a++; //outputs 25
echo $a; //outputs 26
$a = 25;
echo --$a; //outputs 24
$a = 25;
echo $a--; //outputs 25
echo $a; //outputs 24
?>
PHP Logical Operators
The PHP logical operators are used with conditional statements and expressions. So basically conditional operators are used to combine conditional statements.
Operator | Name | Example | Result |
and | And | $a and $b | True if both $a and $b are true |
or | Or | $a or $b | True if either $a or $b is true |
xor | Xor | $a xor $b | True if either $a or $b is true, but not both |
&& | And | $a && $b | True if both $a and $b are true |
|| | Or | $a || $b | True if either $$a or $b is true |
! | Not | !$a | True if $a is not true |
The following example will show you these logical operators in action:
<?php
$bear = 2018;
// Leap years are divisible by 400 or by 4 but not 100
if(($bear % 400 == 0) || (($bear % 100 != 0) && ($bear % 4 == 0))){
echo "$bear is a leap year.";
} else{
echo "$bear is not a leap year.";
}
?>
Output:
2018 is not a leap year.
PHP String Operators
PHP has two operators that are specially designed for strings.
Operator | Description | Example | Result |
. | Concatenation | $str1 . $str2 | Concatenation of $str1 and $str2 |
.= | Concatenation assignment | $str1 .= $str2 | Appends the $str2 to the $str1 |
The following example will show you these string operators in action:
<?php
$a = "try";
$b = " to program";
echo $a . $b;
echo "<br>";
$a .= $b;
echo $a;
?>
Output:
try to program
try to program
PHP Array Operators
The PHP array operators are used to compare arrays.
Operator | Name | Example | Result |
+ | Union | $a + $b | Union of $a and $b |
== | Equality | $a == $b | True if $a and $b have the same key/value pairs |
=== | Identity | $a === $b | True if $a and $b have the same key/value pairs in the same order and of the same types |
!= | Inequality | $a != $b | True if $a is not equal to $b |
<> | Inequality | $a <> $b | True if $a is not equal to $b |
!== | Non-identity | $a !== $b | True if $a is not identical to $b |
The following example will show you these array operators in action:
<?php
$a = array("a" => "Marsh");
$b = array("o" => "Mallow");
$z = $a + $b; // Union of $a and $b
var_dump($z);
echo "<br>";
var_dump($a == $b);
echo "<br>";
var_dump($a === $b);
echo "<br>";
var_dump($a != $b);
echo "<br>";
var_dump($a <> $b);
echo "<br>";
var_dump($a !== $b);
?>
Output:
array(2) { ["a"]=> string(5) "Marsh" ["o"]=> string(6) "Mallow" }bool(false)
bool(false)
bool(true)
bool(true)
bool(true)