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.
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.
Nice one. But i need download examples. That is how i learn.
Good Post For Beginners in PHP
Thanks
helpful post
Thanks for your content. This was really helpful. Hope to find similar contents in near future.