PHP has a variety of operators that can be used in different types of expressions. Here are some examples of common operators in PHP:
Arithmetic operators
Arithmetic operators are used to perform basic arithmetic operations on numerical values.
+
: addition-
: subtraction*
: multiplication/
: division%
: modulus (remainder after division)
$x = 10;
$y = 5;
echo $x + $y; // 15
echo $x - $y; // 5
echo $x * $y; // 50
echo $x / $y; // 2
echo $x % $y; // 0
Assignment operators
Assignment operators are used to assign a value to a variable.
=
: assignment+=
: addition assignment-=
: subtraction assignment*=
: multiplication assignment/=
: division assignment%=
: modulus assignment
$x = 10;
$y = 5;
$x += $y; // $x is now 15
$x -= $y; // $x is now 10
$x *= $y; // $x is now 50
$x /= $y; // $x is now 10
$x %= $y; // $x is now 0
Comparison operators
Comparison operators are used to compare two values and return a Boolean result.
==
: equal to!=
: not equal to>
: greater than<
: less than>=
: greater than or equal to<=
: less than or equal to
$x = 10;
$y = 5;
echo $x == $y; // false
echo $x != $y; // true
echo $x > $y; // true
echo $x < $y; // false
echo $x >= $y; // true
echo $x <= $y; // false
Logical operators
Logical operators are used to perform logical operations on Boolean values.
&&
: and||
: or!
: not
$x = true;
$y = false;
echo $x && $y; // false
echo $x || $y; // true
echo !$x; // false
These are just a few examples of the operators available in PHP. There are many other operators that can be used for different purposes, such as the ternary operator (? :
) for conditional expressions, the bitwise operators for working with binary data, and the type operators for checking the type of a value.