Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
PHP Arithmetic Operators:
+ | Addition |
<?php
$x = 10;
$y = 6;
echo $x + $y;
?>
Output:
16
– | Subtraction |
<?php
$x = 10;
$y = 6;
echo $x – $y;
?>
Output:
4
* | Multiplication |
<?php
$x = 10;
$y = 6;
echo $x * $y;
?>
Output:
60
/ | Division |
<?php
$x = 10;
$y = 6;
echo $x / $y;
?>
Output:
% | Modulus |
<?php
$x = 10;
$y = 6;
echo $x % $y;
?>
Output:
4
PHP Assignment Operators :
The PHP assignment operators are used with numeric values to write a value to a variable.
x = y
<?php
$x = 10;
echo $x;
?>
Output:
10
x += y | x = x + y | Addition |
<?php
$x = 20;
$x += 100;
echo $x;
?>
Output:
120
x -= y | x = x – y | Subtraction |
<?php
$x = 50;
$x -= 30;
echo $x;
?>
Output:
20
x *= y | x = x * y | Multiplication |
<?php
$x = 10;
$y = 6;
echo $x * $y;
?>
Output:
60
x /= y | x = x / y | Division |
<?php
$x = 10;
$x /= 5;
echo $x;
?>
Output:
2
x %= y | x = x % y | Modulus |
<?php
$x = 15;
$x %= 4;
echo $x;
?>
Output:
3
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
== | Equal | $x == $y | Returns true if $x is equal to $y |
<?php
$x = 100;
$y = “100”;
var_dump($x == $y); // returns true because values are equal
?>
Output:
Bool(true)
!= | Not equal | $x != $y | Returns true if $x is not equal to $y |
<?php
$x = 100;
$y = “100”;
var_dump($x != $y); // returns false because values are equal
?>
Output:
Bool(false)
> | Greater than | $x > $y | Returns true if $x is greater than $y |
<?php
$x = 100;
$y = 50;
var_dump($x > $y); // returns true because $x is greater than $y
?>
Output:
bool(true)
< | Less than | $x < $y | Returns true if $x is less than $y |
<?php
$x = 10;
$y = 50;
var_dump($x < $y); // returns true because $x is less than $y
?>
Output:
Bool(true)
>= | Greater than or equal to | $x >= $y | Returns true if $x is greater than or equal to $y |
<?php
$x = 50;
$y = 50;
var_dump($x >= $y); // returns true because $x is greater than or equal to $y
?>
Output:
bool(true)
<= | Less than or equal to | $x <= $y | Returns true if $x is less than or equal to $y |
<?php
$x = 50;
$y = 50;
var_dump($x <= $y); // returns true because $x is less than or equal to $y
?>
Output:
bool(true)
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable’s value.
The PHP decrement operators are used to decrement a variable’s value.
++$x | Pre-increment | Increments $x by one, then returns $x |
<?php
$x = 10;
echo ++$x;
?>
Output:
11
$x++ | Post-increment | Returns $x, then increments $x by one |
<?php
$x = 10;
echo $x++;
?>
Output:
10
–$x | Pre-decrement | Decrements $x by one, then returns $x |
<?php
$x = 10;
echo –$x;
?>
Output:
9
$x– | Post-decrement | Returns $x, then decrements $x by one |
<?php
$x = 10;
echo $x–;
?>
Output:
10
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
&& | And | $x && $y | True if both $x and $y are true |
<?php
$x = 100;
$y = 50;
if ($x == 100 && $y == 50) {
echo “Hello world!”;
}
?>
Output:
Hello world!
|| | Or | $x || $y | True if either $x or $y is true |
<?php
$x = 100;
$y = 50;
if ($x == 100 || $y == 80) {
echo “Hello world!”;
}
?>
Output:
Hello world!
! | Not | !$x | True if $x is not true |
<?php
$x = 100;
if ($x !== 90)
{
echo “Hello world!”;
}
?>
Output:
Hello world!