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

Operator Name Description
a+b Addition Sum of variables a and b, for example 2+3=5
a-b Subtraction Difference of a and b, for example 5-2=3
a*b Multiplication Product of a and b, for example 5*2=10
a/b Division Quotient of a and b, for example 10/2=5
a%b Modulus Remainder of a divided by b, for example 3%2=1
-a Negation Opposite of x, for example -5
a.b Concatenation Used 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

Operator Description
a=b The value of b will be set to a, the left side can be expressions also, for example a= 10-5
a+=b Same as a=a+b
a-=b Same as a=a-b
a*=b Same as a=a*b
a/=b Same as a=a/b
a%=b Same as a=a%b
a.=b Same 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

Operator Name Description
++a Pre-increment Increment a by 1 and then returns it
a++ Post-increment returns a and then increment it by 1
–a Pre-decrement Decrement a by 1 and then returns it
a– Post-decrement returns 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

Operator Name Description
a==b Equal True if a is equal to b
a===b Identical True if a is equal to b and of same type, 5===”5” is false
a!=b Not equal True if a and b are not equal
a<>b Not equal True if a and b are not equal
a!==b Not identical True if a and b are not equal and they are not of same type, for example 5!==”5″ returns true
a<b Less than True if a is less than b
a>b Greater than True if a is greater than b
a<=b Less than or equal to True if a is less than or equal to b
a>=b Greater than or equal to True 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

Operator Name Description
a and b And True if both a and b are true
a or b Or True if either a or b is true
a xor b Xor True if either a or b is true, but not both
!a Not True if a is not true
a && b And Same as and operator
a || b Or Same 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

Operator Name Description
a & b And Bits that are set in both a and b are set.
a | b Or Bits that are set in either a or b are set
a ^ b Xor Bits that are set in a or b but not both are set
~ a Not Bits that are set in a are not set, and vice versa
a << b Shift left Shift the bits of a, b steps to the left
a >> b Shift right Shift 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

Operator Name Description
a + b Union Union of a and b
a == b Equality True if a and b have same key/value pairs
a === b Identity True if a and b have the same key/value pairs in the same order and of the same types.
a != b Inequality True if a is not equal to b
a <> b Inequality True if a is not equal to b
a !== b Non-Identity True 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. Aqeel says:

    Thanks

  4. WAseem Asgar says:

    helpful post

  5. 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