PHP Operators
PHP Arithmetic Operators
The example below shows the different results of using
the different arithmetic operators: Example <?php $x=10; $y=6; echo ($x + $y); // outputs 16 echo ($x - $y); // outputs 4 echo ($x * $y); // outputs 60 echo ($x / $y); // outputs 1.6666666666667 echo ($x % $y); // outputs 4 ?>
16
4 60 1.6666666666667 4
PHP Assignment Operators
The PHP assignment operators is used to write a
value to a variable. The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.
The example below shows the different results of using the different assignment operators:
<?php $x=10; echo $x; // outputs 10 $y=20; $y += 100; echo $y; // outputs 120 $z=50; $z -= 25; echo $z; // outputs 25 $i=5; $i *= 6; echo $i; // outputs 30 $j=10; $j /= 5; echo $j; // outputs 2 $k=15; $k %= 4; echo $k; // outputs 3 ?>
10
120 25 30 2 3
PHP String Operators
<?php
$a = "Hello"; $b = $a . " world!"; echo $b; // outputs Hello world!
echo "<br>"; $x="Hello"; $x .= " world!"; echo $x; // outputs Hello world!
Hello world!
Hello world!
PHP Increment / Decrement Operators
The example below shows the different results of using the different
increment/decrement operators: Example:
<?php
$x=10; echo ++$x; // outputs 11 $y=10; echo $y++; // outputs 10 $z=5; echo --$z; // outputs 4 $i=5; echo $i--; // outputs 5
11
10 4 5
PHP Comparison Operators
-The PHP comparison operators are used to compare two values (number or string):
<?php
$x=100; $y="100"; var_dump($x == $y); echo "<br>"; var_dump($x === $y); echo "<br>"; var_dump($x != $y); echo "<br>"; var_dump($x !== $y); echo "<br>"; $a=50; $b=90;
var_dump($a > $b); echo "<br>"; var_dump($a < $b); ?>
bool(true)
bool(false) bool(false) bool(true) bool(false) bool(true)
PHP Logical Operators
PHP Array Operators
The PHP array operators are used to compare arrays:
The example below shows the different results of using
the different array operators: <?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); $z = $x + $y; // union of $x and $y var_dump($z); var_dump($x == $y); var_dump($x === $y); var_dump($x != $y); var_dump($x <> $y); var_dump($x !== $y); ?>
array(4) { ["a"]=> string(3) "red" ["b"]=> string(5)
"green" ["c"]=> string(4) "blue" ["d"]=> string(6) "yellow" } bool(false) bool(false) bool(true) bool(true) bool(true)