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

8 Comments

  • Ivan says:

    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.

  • Mila says:

    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! 👍

  • Ivy says:

    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!

  • Nora says:

    Great article! I have always been confused with how to use PHP operators, but this tutorial has cleared things up for me . Now I can confidently use the correct operators in my code without any hessitation. Thank you for the detailed explanations and examples, they were really helpfull!

  • Alexis says:

    Wow, this tutorial on how to use PHP operators is absolutely fantastic! I’ve been struggling with understanding operators in PHP for quite some time, but thanks to this clear and concise explanation, everything finally makes sense to me . The way the information is presented, with helpful examples and explanations, really helped me grasp the concept easily. Now I feel more confident in my PHP coding skills. Keep up the great work!

  • Addison says:

    This tutorial really helped me understand the different PHP operators and how to use them effectively in my code . I’ve always been a bit confused about the various operators, but this explanation made it easy to grasp. Now I feel confident in using operators such as arithmetic, assignment, logical, and comparison in my projects. Thank you for breaking it down in such a user-friendly way!

  • Lillian says:

    I found this tutorial really helpful in understanding how to use PHP operators . The explanations were easy to follow and I feel more confident in my coding abilities now. Thank you for sharing this information!

  • George says:

    This tutorial was incredibly helpful in understanding how to use different operators in PHP . I feel much more confident in my coding abilities now. Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *