Operators in PHP

Operators in PHP are used to perform different operations on operands. Operators can be categorized in following types :
  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators

Arithmetic operators : Arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Assume variable $x holds 20 and variable $y holds 10, then :

Operators Name Description Example
+ Addition Sum of $x and $y $x + $y =30
- Subtraction Difference of $x and $y $x - $y = -10
* Multiplication Products of $x and $y $x * $y = 200
/ Division Quotient of $x and $y $x / $y = 2
% Modulus Remiander of $x divided by $y $x % $y = 0
** Exponentiation Result of raising $x to the $y'th power $x ** $y = 10240000000000

Example :
<?php
$a = 12;
$b = 8;
$c = $a + $b;
echo $c;
?>

Output :
20


Assignment operators : Assignment operators are used with numeric values to write a value to a variable.

Shorthand Expression Description
$x += $y $x = $x + $y Addition
$x -= $y $x = $x - $y Subtraction
$x *= $y $x = $x * $y Multiplication
$x /= $y $x = $x / $y Division
$x %= $y $x = $x % $y Modulus

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

$x += $y;
echo $x."<br>";
$x -= $y;
echo $x."<br>";
$x *= $y;
echo $x."<br>";
$x /= $y;
echo $x."<br>";
$x %= $y;
echo $x;
?>

Output :
15
10
50
10
0

Comparison operators : Comparison operators are used in PHP to compare two values (number or string).  Assume variable $x holds 20 and variable $y holds 10, then :

Operators Name Description Example
== Equal Returns true if $x is equal to $y ($x == $y) is false
=== Identical Returns true if $x is equal to $y, and they are of the same type ($x === $y) is false
!= Not equal Returns true if $x is not equal to $y ($x != $y) is true
<> Not equal Returns true if $x is not equal to $y ($x <> $y) is true
!== Not identical Returns true if $x is not equal to $y, or they are not of the same type ($x !== $y) is true
> Greater than Returns true if $x is greater than $y ($x > $y) is true
< Less than Returns true if $x is less than $y ($x < $y) is false
>= Greater than or equal to Returns true if $x is greater than or equal to $y ($x >= $y) is true
<= Less than or equal to Returns true if $x is less than or equal to $y ($x < $y) is false


Increment/Decrement operators : In PHP increment operators are used to increment a variable's value and decrement operators are used to decrement a variable's value.

Operator Name Description
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one

Example :
<?php
$x = 10;
echo ++$x."<br>";
$y = 20;
echo $y++."<br>";
$a = 15;
echo --$a."<br>";
$b = 25;
echo $b--;
?>

Output :
11
20
14
25


Logical operators : Logical operators are used to combine conditional statements.

Operators Name Expression Description
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

Example :
<?php
$x = 10;
$y=20;

if($x==10 and $y==20)
{
    echo "True"."<br>";
}

if($x==10 or $y==30)
{
    echo "True"."<br>";
}

if($x==10 and $y==20)
{
    echo "False"."<br>";
}

if($x !==20)
{
    echo "True"."<br>";
}
?>

Output :
True
True
False
True


String operators :

Operator Name Expression Description
. Concatenation $txt_1 . $txt_2 Concatenation of $txt1 and $txt2
.= Concatenation assignment $txt_1 .= $txt_2 Appends $txt_2 to $txt_1

Example :
<?php
$txt_1 = "Hello";
$txt_2 = " world";
echo $txt_1 . $txt_2."<br>";

$txt_3 = "My";
$txt_4= " PHP";
$txt_3 .= $txt_4;
echo $txt_3;
?>

Output :
Hello world
My PHP


Array operators : Array operators are used in PHP to compare arrays.


Operator Name Expression Description
+ Union $x + $y Union of $x and $y
== Equality $x == $y Returns true if $x and $y have the same key/value pairs
=== Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types
!= Inequality $x != $y Returns true if $x is not equal to $y
<> Inequality $x <> $y Returns true if $x is not equal to $y
!== Non-identity $x !== $y Returns true if $x is not identical to $y


Example :
<?php
$x = array("a" => "red", "b" => "green"); 
$y = array("c" => "blue", "d" => "yellow"); 
print_r($x + $y);
?>

Output :
Array ( [a] => red [b] => green [c] => blue [d] => yellow )

0 Comments