PHP Operators

Filed Under: PHP

Today we will look into PHP operators. Earlier we went through PHP tutorial for beginners to get you started with variables and String.

PHP Operators

We can use PHP operators with String, integers, boolean and arrays.

php operators

PHP Operator Types

PHP scripting language provides different kinds of operators to work with variables such as arithmetic operators, comparison and logical operators.

1. PHP Arithmetic Operators

OperatorNameDescription
a+bAdditionSum of variables a and b, for example 2+3=5
a-bSubtractionDifference of a and b, for example 5-2=3
a*bMultiplicationProduct of a and b, for example 5*2=10
a/bDivisionQuotient of a and b, for example 10/2=5
a%bModulusRemainder of a divided by b, for example 3%2=1
-aNegationOpposite of x, for example -5
a.bConcatenationUsed to concat, for example “Pankaj” . “Kumar”=”PankajKumar”

<?php

$a = 10;
$b = 5;
$c = 3;
$d = 8;

//addition example
$add=$a + $b;
echo $add; //prints 15
echo "<br>";

//subtraction example
$sub = $a - $b;
echo $sub; //prints 5
echo "<br>";

//multiplication example
$mul = $a * $b;
echo $mul; //prints 50
echo "<br>";

//division example
$div = $a / $b;
echo $div; // prints 2
echo "<br>";
echo $a/$c; //prints 3.3333333333333
echo "<br>";
echo $d/$c; //prints 2.6666666666667
echo "<br>";

//modulus example
$mod= $a % $b;
echo $mod;
echo "<br>"; //prints 0

//Negation example
$neg = -$a;
echo $neg; //prints -10;
echo "<br>";

//Concatenation example
$str1="Pankaj";
$str2="Kumar";

echo $str1 . " " . $str2; //prints "Pankaj Kumar"
echo "<br>";

echo $a . $b; //prints 105
echo "<br>";

echo $c . $d; //prints 38
?> 

2. PHP Assignment Operators

OperatorDescription
a=bThe value of b will be set to a, the left side can be expressions also, for example a= 10-5
a+=bSame as a=a+b
a-=bSame as a=a-b
a*=bSame as a=a*b
a/=bSame as a=a/b
a%=bSame as a=a%b
a.=bSame as a=a.b

<?php

$a=12;

//assignment operator example
$b=$a;
echo $b; //prints 12
echo "<br>";

$a+=$b;
echo $a; //prints 24, same as a=a+b
echo "<br>";

$a-=$b;
echo $a; //prints 12, same as a=a-b=24-12
echo "<br>";

$a*=$b;
echo $a; //prints 144, same as a=a*b=12*12
echo "<br>";

$a/=$b;
echo $a; //prints 12, same as a=a/b=144/12
echo "<br>";

$a%=$b;
echo $a; //prints 0, same as a=a%b=12%12
echo "<br>";

$a.=$b;
echo $a; //prints 012, same as a=a.b=0.12=012

?> 

3. PHP Increment/Decrement Operators

OperatorNameDescription
++aPre-incrementIncrement a by 1 and then returns it
a++Post-incrementreturns a and then increment it by 1
–aPre-decrementDecrement a by 1 and then returns it
a–Post-decrementreturns a and then decrement it by 1

<?php

$a=12;

//Pre-increment example
$b=++$a;
echo "a=".$a." ,b=".$b; //prints a=13 ,b=13
echo "<br>";

//Post-increment example
$b=$a++;
echo "a=".$a." ,b=".$b; //prints a=14 ,b=13
echo "<br>";

//Pre-decrement example
$b=--$a;
echo "a=".$a." ,b=".$b; //prints a=13 ,b=13
echo "<br>";

//Post-decrement example
$b=$a--;
echo "a=".$a." ,b=".$b; //prints a=12 ,b=13
echo "<br>";

?> 

4. PHP Comparison Operators

OperatorNameDescription
a==bEqualTrue if a is equal to b
a===bIdenticalTrue if a is equal to b and of same type, 5===”5” is false
a!=bNot equalTrue if a and b are not equal
a<>bNot equalTrue if a and b are not equal
a!==bNot identicalTrue if a and b are not equal and they are not of same type, for example 5!==”5″ returns true
a<bLess thanTrue if a is less than b
a>bGreater thanTrue if a is greater than b
a<=bLess than or equal toTrue if a is less than or equal to b
a>=bGreater than or equal toTrue if a is greater than or equal to b

<?php

$a=10;
$b="10";
$c=10;
$d=5;

//Equal example
var_dump($a==$b); //prints bool(true) 
echo "<br>";
var_dump($a==$c); //prints bool(true) 
echo "<br>";

//Identical example
var_dump($a===$b); //prints bool(false) 
echo "<br>";
var_dump($a===$c); //prints bool(true) 
echo "<br>";

//Not equal example
var_dump($a!=$b); //prints bool(false) 
echo "<br>";
var_dump($a!=$c); //prints bool(false) 
echo "<br>";
var_dump($a<>$b); //prints bool(false) 
echo "<br>";
var_dump($a<>$c); //prints bool(false) 
echo "<br>";

//Not identical example
var_dump($a!==$b); //prints bool(true) 
echo "<br>";
var_dump($a!==$c); //prints bool(false) 
echo "<br>";

//Less than example
var_dump($a<$b); //prints bool(false) 
echo "<br>";
var_dump($a<$d); //prints bool(false) 
echo "<br>";

//Greater than example
var_dump($a>$b); //prints bool(false) 
echo "<br>";
var_dump($a>$d); //prints bool(true) 
echo "<br>";

//Less than or equal to example
var_dump($a<=$b); //prints bool(true) 
echo "<br>";
var_dump($a<=$d); //prints bool(false) 
echo "<br>";

//Greater than or equal to example
var_dump($a>=$b); //prints bool(true) 
echo "<br>";
var_dump($a>=$d); //prints bool(true) 
echo "<br>";

?> 

5. PHP Logical Operators

OperatorNameDescription
a and bAndTrue if both a and b are true
a or bOrTrue if either a or b is true
a xor bXorTrue if either a or b is true, but not both
!aNotTrue if a is not true
a && bAndSame as and operator
a || bOrSame as or operator

<?php

$a=true;
$b=false;
$c=true;

//And example
var_dump($a and $b); //prints bool(false)
var_dump($a and $c); //prints bool(true)

var_dump($a && $b); //prints bool(false)
var_dump($a && $c); //prints bool(true)

echo "<br>";

//Or example
var_dump($a or $b); //prints bool(true)
var_dump($a or $c); //prints bool(true) 

var_dump($a || $b); //prints bool(true)
var_dump($a || $c); //prints bool(true)
echo "<br>";

//Xor example
var_dump($a xor $b); //prints bool(true)
var_dump($a xor $c); //prints bool(false)
echo "<br>";

//Not example
var_dump(!$a); //prints bool(false)
var_dump(!$b); //prints bool(true)
echo "<br>";

?> 

6. PHP Bitwise Operators

OperatorNameDescription
a & bAndBits that are set in both a and b are set.
a | bOrBits that are set in either a or b are set
a ^ bXorBits that are set in a or b but not both are set
~ aNotBits that are set in a are not set, and vice versa
a << bShift leftShift the bits of a, b steps to the left
a >> bShift rightShift the bits of a, b steps to the right

<?php

$a=15; // 00001111
$b=35; // 00100011

//Bitwise And example
$c=$a & $b;
//	00001111
// &00100011
//  00000011

echo $c; //prints 3
echo "<br>";

//Bitwise Or example
$c=$a | $b;
//	00001111
// |00100011
//  00101111

echo $c; //prints 47
echo "<br>";

//Bitwise Xor example
$c=$a ^ $b;
//	00001111
// ^00100011
//  00101100

echo $c; //prints 44
echo "<br>";

//Bitwise Not example
$c=$a & ~$b; //bits that are set in a but not in b, 00001100 = 12
$d=$b & ~$a; //bits that are set in b but not in a, 00100000 = 32

echo $c; //prints 12
echo "<br>";
echo $d; //prints 32
echo "<br>";

//Shift left example
$c=$a<<2; //bits shifted to left two places, 00111100 = 60
echo $c; //prints 60
echo "<br>";

//Shift right example
$c=$a>>2; //bits shifted to right two places, 00000011 = 3
echo $c; //prints 3

?>

7. PHP Array Operators

OperatorNameDescription
a + bUnionUnion of a and b
a == bEqualityTrue if a and b have 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

<?php

$cars=array("BMW","Toyota","Honda");
$buses=array("BMW","Volvo","Tata","Mercedez");

//Union of Arrays
$cars_buses = $cars + $buses;
$arrlength=count($cars_buses);

for($x=0;$x<$arrlength;$x++)
  {
  echo $cars_buses[$x] . " "; //prints "BMW Toyota Honda Mercedez "
  }
echo "<br>";

//Arrays Equality
$cars1=array("BMW","2" => "Honda",1 => "Toyota");
var_dump($cars == $cars1); //prints bool(true);
var_dump($cars == $buses); //prints bool(false);
echo "<br>";

//Identical Arrays operator
var_dump($cars === $cars1); //prints bool(false);
var_dump($cars === $buses); //prints bool(false);
echo "<br>";

//Inequality example
var_dump($cars != $cars1); //prints bool(false);
var_dump($cars != $buses); //prints bool(true);
echo "<br>";

var_dump($cars <> $cars1); //prints bool(false);
var_dump($cars <> $buses); //prints bool(true);
echo "<br>";

//Non-Identical Arrays operator
var_dump($cars !== $cars1); //prints bool(true);
var_dump($cars !== $buses); //prints bool(true);
echo "<br>";

?> 

8. PHP Type Operator

instanceof is the type operator used to determine if a PHP variable is an instantiated object of a class or not.


<?php
class MyClass{}

class OtherClass{}

$var = new MyClass;

var_dump($var instanceof MyClass); // prints bool(true)
var_dump($var instanceof OtherClass); // prints bool(false)

?> 

That’s all for PHP operators. In the next tutorial, we will learn about PHP Conditional Statements, Arrays and Loops in PHP.

Comments

  1. slim says:

    Nice one. But i need download examples. That is how i learn.

  2. Gaurang vyas says:

    Good Post For Beginners in PHP

  3. WAseem Asgar says:

    helpful post

  4. Santosh Tewari says:

    Thanks for your content. This was really helpful. Hope to find similar contents in near future.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages