Php Operators

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
  2. PHP Assignment Operators
  3. PHP Increment/Decrement Operators
  4. PHP Comparison Operators
  5. PHP Logical Operators
  6. PHP Bitwise Operators
  7. PHP Array Operators
  8. PHP Type Operator

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 15echo "<br>";//subtraction example$sub = $a - $b;echo $sub; //prints 5echo "<br>";//multiplication example$mul = $a * $b;echo $mul; //prints 50echo "<br>";//division example$div = $a / $b;echo $div; // prints 2echo "<br>";echo $a/$c; //prints 3.3333333333333echo "<br>";echo $d/$c; //prints 2.6666666666667echo "<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 105echo "<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 12echo "<br>";$a+=$b;echo $a; //prints 24, same as a=a+becho "<br>";$a-=$b;echo $a; //prints 12, same as a=a-b=24-12echo "<br>";$a*=$b;echo $a; //prints 144, same as a=a*b=12*12echo "<br>";$a/=$b;echo $a; //prints 12, same as a=a/b=144/12echo "<br>";$a%=$b;echo $a; //prints 0, same as a=a%b=12%12echo "<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=13echo "<br>";//Post-increment example$b=$a++;echo "a=".$a." ,b=".$b; //prints a=14 ,b=13echo "<br>";//Pre-decrement example$b=--$a;echo "a=".$a." ,b=".$b; //prints a=13 ,b=13echo "<br>";//Post-decrement example$b=$a--;echo "a=".$a." ,b=".$b; //prints a=12 ,b=13echo "<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 examplevar_dump($a==$b); //prints bool(true) echo "<br>";var_dump($a==$c); //prints bool(true) echo "<br>";//Identical examplevar_dump($a===$b); //prints bool(false) echo "<br>";var_dump($a===$c); //prints bool(true) echo "<br>";//Not equal examplevar_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 examplevar_dump($a!==$b); //prints bool(true) echo "<br>";var_dump($a!==$c); //prints bool(false) echo "<br>";//Less than examplevar_dump($a<$b); //prints bool(false) echo "<br>";var_dump($a<$d); //prints bool(false) echo "<br>";//Greater than examplevar_dump($a>$b); //prints bool(false) echo "<br>";var_dump($a>$d); //prints bool(true) echo "<br>";//Less than or equal to examplevar_dump($a<=$b); //prints bool(true) echo "<br>";var_dump($a<=$d); //prints bool(false) echo "<br>";//Greater than or equal to examplevar_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 examplevar_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 examplevar_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 examplevar_dump($a xor $b); //prints bool(true)var_dump($a xor $c); //prints bool(false)echo "<br>";//Not examplevar_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// 00000011echo $c; //prints 3echo "<br>";//Bitwise Or example$c=$a | $b;// 00001111// |00100011// 00101111echo $c; //prints 47echo "<br>";//Bitwise Xor example$c=$a ^ $b;// 00001111// ^00100011// 00101100echo $c; //prints 44echo "<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 = 32echo $c; //prints 12echo "<br>";echo $d; //prints 32echo "<br>";//Shift left example$c=$a<<2; //bits shifted to left two places, 00111100 = 60echo $c; //prints 60echo "<br>";//Shift right example$c=$a>>2; //bits shifted to right two places, 00000011 = 3echo $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 operatorvar_dump($cars === $cars1); //prints bool(false);var_dump($cars === $buses); //prints bool(false);echo "<br>";//Inequality examplevar_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 operatorvar_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.

<?phpclass 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.

  • 109