How to use PHP operators
Operators are used to perform operations on variables and values. PHP has many operators, including arithmetic, assignment, comparison, and logical operators.
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations. The following table shows the arithmetic operators in PHP:
Operator | Description | Example |
---|---|---|
+ | Addition |
$x = 10; $y = 20; $z = $x + $y; // $z = 30 |
– | Subtraction |
$x = 10; $y = 20; $z = $y - $x; // $z = 10 |
* | Multiplication |
$x = 5; $y = 6; $z = $x * $y; // $z = 30 |
/ | Division |
$x = 12; $y = 6; $z = $x / $y; // $z = 2 |
% | Modulus |
$x = 10; $y = 3; $z = $x % $y; // $z = 1 |
** | Exponentiation |
$x = 2; $y = 3; $z = $x ** $y; // $z = 8 |
Assignment Operators
Assignment operators are used to assign values to variables. The following table shows the assignment operators in PHP:
Operator | Description | Example |
---|---|---|
= | Assignment |
$x = 10; |
+= | Addition assignment |
$x = 10; $x += 5; // $x = 15 |
-= | Subtraction assignment |
$x = 10; $x -= 5; // $x = 5 |
*= | Multiplication assignment |
$x = 10; $x *= 5; // $x = 50 |
/= | Division assignment |
$x = 10; $x /= 5; // $x = 2 |
%= | Modulus assignment |
$x = 10; $x %= 3; // $x = 1 |
Comparison Operators
Comparison operators are used to compare values. The following table shows the comparison operators in PHP:
Operator | Description | Example |
---|---|---|
== | Equal to |
$x = 10; $y = 10; if ($x == $y) { echo "x is equal to y"; } |
!= | Not equal to |
$x = 10; $y = 5; if ($x != $y) { echo "x is not equal to y"; } |
> | Greater than |
$x = 10; $y = 5; if ($x > $y) { echo "x is greater than y"; } |
< | Less than |
$x = 5; $y = 10; if ($x < $y) { echo "x is less than y"; } |
>= | Greater than or equal to |
$x = 10; $y = 10; if ($x >= $y) { echo "x is greater than or equal to y"; } |
<= | Less than or equal to |
$x = 5; $y = 10; if ($x <= $y) { echo "x is less than or equal to y"; } |
More examples
Find more examples and operators on php.net.
Written by: Steven Iversen 25-05-2023 Written in: PHP tutorials
pretty useful for learning PHP operaturs. I can now do calculation using the correct operater which was my major challenge. The step by step guide was easy to understand, but I thinks it could have been better if the author used more examples for each operater.
This tutorial was extremely helpful! I’ve always struggled with understanding PHP operators 😅, but this article broke it down in such a simple and concise way . Now I feel much more confident in using them correctly in my code. Thank you for sharing it with us! 👍
This tutorial on using PHP operators is incredibly helpful! I’ve always found it a bit confusing to understand the different operator types and their functions, but this article breaks it down in a simple and understandable way . The clear examples and explanations make it much easier for beginners like me to grasp the concept. I can’t wait to implement these operators into my PHP projects. Great job on this tutorial!