PHP Operators


Operators function as symbols that execute operations on variables and values.


PHP operators function in arithmetic calculations and enable comparisons, logical decisions and variable assignments among other operations.

php-operators

Arithmetic Operators

Used to perform basic mathematical operations.

 

Operator Name Example Result
+ Addition $x + $y Sum
- Subtraction $x - $y Difference
* Multiplication $x * $y Product
/ Division $x / $y Quotient
% Modulus $x % $y Remainder
** Exponentiation $x ** $y Power

Assignment Operators

Used to assign values to variables.

 

Operator Description Example
= Assign $x = 5;
+= Add and assign $x += 5;
-= Subtract and assign $x -= 2;
*= Multiply and assign $x *= 3;
/= Divide and assign $x /= 2;
%= Modulus and assign $x %= 3;

Comparison Operators

Used to compare values.

 

Operator Description Example Result
== Equal $x == $y true/false
=== Identical (value + type) $x === $y true/false
!= Not equal $x != $y true/false
!== Not identical $x !== $y true/false
> Greater than $x > $y true/false
< Less than $x < $y   true/false
>= Greater than or equal to $x >= $y true/false
<= Less than or equal to $x <= $y true/false

Example

  <?php
$x = 10;
$y = 5;

// Arithmetic
echo "Sum: " . ($x + $y) . "<br>";

// Assignment
$x += 5;
echo "After += : $x<br>";

// Comparison
echo ($x == $y) ? "Equal<br>" : "Not equal<br>";

// Logical
if ($x > 0 && $y > 0) {
    echo "Both positive<br>";
}

// String
$greeting = "Hello, ";
$name = "Alice";
echo $greeting . $name . "<br>";
?>



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.