PHP Operators

  • Arithmetic Operators
  • Assignment Operators
  • Bitwise Operators
  • Comparison Operators
  • Error Control Operators
  • Execution Operators
  • Incrementing/Decrementing Operators
  • Logical Operators
  • String Operators
  • Array Operators
  • Type Operators

PHP Arithmetic Operators

Please check the following Operators with $a=10 , $b=4

Operator Example Result Name Description
+$a + $b14AdditionSum of $a and $b
-$a - $b4SubtractionDifference of $a and $b
*$a * $b40MultiplicationProduct of $a and $b
/$a / $b2.5DivisionQuotient of $a and $b
%$a % $b2ModuloRemainder of $a divided by $b
**$a ** $b10000ExponentiationResult of raising $a to the $b'th power
++$a10IdentityConversion of $a to int or float
--$a-10NegationOpposite of $a

PHP Assignment Operators

The PHP assignment operator is "+". In assignment operator value is assigned like $a=10 , $b=4.

Example Same As Result Description
$a = $b$a = $b 4The left operand get the value of right operand
$a += $b $a = $a + $b 14Addition
$a -= $b $a = $a - $b 6Subtraction
$a *= $b $a = $a * $b 40Multiplication
$a /= $b $a = $a / $b 2.5Division
$a %= $b $a = $a % $b 2Modulus

PHP Bitwise Operators

Result with $a=10 , $b=4.

Operator Example Result Name Description
& $a & $b0AndBits that are set in both $a and $b are set
| $a | $b14OrBits that are set in either $a or $b are set.
^ a ^ b14XorBits that are set in $a or $b but not both are set
~ ~ $a-11NotBits that are set in $a are not set, and vice versa
<< $a << $b160Shift leftShift the bits of $a $b steps to the left
<< $a >> $b0Shift rightShift the bits of $a $b steps to the right

PHP Comparison Operators

Operator Example Name Description
==X == YEqualTRUE if X is equal to Y
===X === YIdenticalTRUE if X is equal to Y, and they are of the same data type(int,string,float,array,object etc)
!=X != YNot equalTRUE if X is not equal to Y after type juggling.
<>X <> YNot equalTRUE if X is not equal to Y after type juggling.
!==X !== YNot identicalTRUE if X is not equal to Y, or they are not of the same data type(int,string,float,array,object etc).
<X < YLess thanTRUE if X is strictly less than Y.
>X > YGreater thanTRUE if X is strictly greater than Y.
<=X <= YLess than or equal toTRUE if X is less than or equal to Y.
>=X >= YGreater than or equal toTRUE if X is greater than or equal to Y.
<=>X <=> YSpaceshipTRUE An integer less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively.

PHP Error Control Operators

The sign of error control operator is (@), it's used to hide the error messages that might be generated by that expression

Example 1

echo $string; 
Output:: it shows Notice:Undefined variable

Example 2

echo @$string; 
Output::It does not show any such type of "Notice"

PHP Execution Operators

The sign of execution operator is backticks (``), it's used to execute as a shell command

Example 1

$website = 'www.example.com';
echo `ping -n 5 {$website}`;

Example 2

echo @$string; 
Output::It does not show any type of "Notice"

PHP Incrementing/Decrementing Operators

PHP Incrementing/Decrementing operators are used to increment/decrement a variable's value.

Result with $a=5; $b=10; $c=20; $d=25;.

Operator Result Name Description
++$a6Pre-incrementIncrements $a by one, then returns $a.
$b++10Post-incrementReturns $b, then increments $a by one.
--$c19Pre-decrementDecrements $c by one, then returns $c.
$d--25Post-decrement Returns $d, then decrements $d by one.

PHP Logical Operators

Operator Example Name Description
and$a and $bAndTRUE if both $a and $b are TRUE
or$a or $bOrTRUE if either $a or $b is TRUE
xor$a xor $bXorTRUE if either $a or $b is TRUE, but not both.
!! $aNotTRUE if $a is not TRUE
&&$a && $bAndTRUE if both $a and $b are TRUE
||$a || $bOrTRUE if either $a or $b is TRUE

PHP String Operators

PHP has two String operators only, first is the concatenation operator ('.') and second is the concatenating assignment operator ('.=')

Example 1 - PHP concatenation operator ('.')

$string1 = "testing";
$string2 = "I am ".$string1;
echo $string2;
//Output :: I am testing

Example 2 - PHP concatenating assignment operator ('.=')

$string1 = "I am";
$string1 .= " testing";
echo $string1;
//Output :: I am testing

PHP Array Operators

Operator Example Name Description
+$a + $bUnionUnion of $a and $b
==$a == $bEqualityTRUE if $a and $b have the same key/value pairs.
===$a === $bIdentityTRUE if $a and $b have the same key/value pairs in the same order and of the same types.
!=$a != $bInequalityTRUE if $a is not equal to $b
<>$a <> $bInequalityTRUE if $a is not equal to $b
!==$a |== $bNon-identityTRUE if $a is not identical to $b
$a = array("a" => "PHP", "c" => "JS");
$b = array("a" => "HTML", "b" => "CSS", "d" => "Python");
$c = $a + $b; // Union of $a and $b
var_dump($c);

//Output ::
array (size=4)
  'a' => string 'PHP' (length=3)
  'c' => string 'JS' (length=2)
  'b' => string 'CSS' (length=3)
  'd' => string 'Python' (length=6)

PHP Type Operators

Example 1 - PHP instanceof operator with classes

class classA 
{
}
class classB 
{
}
$a = new classA;
var_dump($a instanceof classA); // true
var_dump($a instanceof classB); // false

Example 2 - PHP instanceof operator with inherited classes

class classA 
{ 
}
class classB extends classA
{
}
$a = new classB;
var_dump($a instanceof classA); // true
var_dump($a instanceof classB); // true

Example 3 - PHP instanceof operator with interfaces

interface interfaceA
{ 
}
class classB implements interfaceA
{	
}
$a = new classB;
var_dump($a instanceof classB); // true
var_dump($a instanceof interfaceA); // true

Example 4 - PHP instanceof with other variables

interface interfaceA
{
}
class classB implements interfaceA
{
}
$a = new classB;
$b = new classB;
$c = 'classB';
$d = 'NotClass';
var_dump($a instanceof $a); // true
var_dump($a instanceof $b); // true
var_dump($a instanceof $c); // true
var_dump($a instanceof $d); // false