PHP – Operators
PHP Arithmetic Operators
<?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 ?>
Assignement 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 ?>
String Operators
<?php $a = "Hello"; $b = $a . " world!"; echo $b; // outputs Hello world! $x="Hello"; $x .= " world!"; echo $x; // outputs Hello world! ?>
Increment/Decrement Operators
<?php $x=10; echo ++$x; // outputs 11 first -> the increment starts echo ++$x; // outputs 12 -> the incrementt continues... $y=10; echo $y++; // outputs 10 first -> the value in the same echo $y++; // outputs 11 second -> the increment starts $z=5; echo --$z; // outputs 4 first -> the decrement starts echo --$z; // outputs 3 second -> the decrement continues... $i=5; echo $i--; // outputs 5 first -> the value in the same echo $i--; // outputs 4 second -> the decrement starts ?>
Comparison Operators
== Equal $x == $y True if $x is equal to $y
=== Identical $x === $y True if $x is equal to $y, and they are of the same type
!= Not equal $x != $y True if $x is not equal to $y
<> Not equal $x <> $y True if $x is not equal to $y
!== Not identical $x !== $y True if $x is not equal to $y, or they are not of the same type
> Greater than $x > $y True if $x is greater than $y
< Less than $x < $y True if $x is less than $y
>= Greater than or equal to $x >= $y True if $x is greater than or equal to $y
<= Less than or equal to $x <= $y True if $x is less than or equal to $y
[php]
<?php
$x=100; // It is an integer number
$y="100"; // It is a string
var_dump($x == $y); // Output: bool(true)
echo "<br>";
var_dump($x === $y); // Output: bool(false)
echo "<br>";
var_dump($x != $y); // Output: bool(false)
echo "<br>";
var_dump($x !== $y); // Output: bool(true)
echo "<br>";
$a=50;
$b=90;
var_dump($a > $b); // Output: bool(false)
echo "<br>";
var_dump($a < $b); // Output: bool(true)
?>
[/php]
Logical Operators
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true
Array Operators
+ Union $x + $y Union of $x and $y (but duplicate keys are not overwritten)
== Equality $x == $y True if $x and $y have the same key/value pairs
=== Identity $x === $y True if $x and $y have the same key/value pairs in the same order and of the same types
!= Inequality $x != $y True if $x is not equal to $y
<> Inequality $x <> $y True if $x is not equal to $y
!== Non-identity $x !== $y True if $x is not identical to $y
<?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); $z = $x + $y; var_dump($z); // Output: array(4) { ["a"]=> string(3) "red" ["b"]=> string(5) "green" ["c"]=> string(4) "blue" ["d"]=> string(6) "yellow" } var_dump($x == $y); // Output: bool(false) var_dump($x === $y); // Output: bool(false) var_dump($x != $y); // Output: bool(true) var_dump($x <> $y); // Output: bool(true) var_dump($x !== $y); // Output: bool(true) ?>