Loading...

PHP Comparison Operators


in this tutorials we are going to learn about php comparison operators like === in php, == in php, !== in php, != in php, <, >, <>, <= , >= in php


List of comparison operators in php


in php comparison operators are used to compare two variable values based on available comparison operators are :
Operator Name
== Equal
=== Identical
!= Not Equal
!== Not Identical
< Less than
> Greater than
<> Not Equal
<= Less than Or Equal to
>= Greater than or Equal to


Equal Operator | == in php


in php == operator is used to check two variables value are equal.
it return true when both side values are equal. otherwise it returns false if both side values are no equal.
for example:
        		     					    		
$var1 = 10;

$var2 = "10";

// if statement executes when expression returns true.

if($var1 == $var2){
	echo "values matched";
}
else{
	echo "values not matched";
}

output:

values matched
        		     					    		
        		     					    	
in the above example $var1 value is 10 (without quotes) and $var2 value is "10" (with quotes).
in php int value is defined without quotes and string value is defined within quotes.
but in the above case when the two values are evaluated in if conditional .
both side values are equal because php perform implicit type conversion to infer the var value.


Identical Operator | === in php


in php === operator is used to check two variables value and their types are equal.
it returns true if the variable data type and their value is equal and same on both sides. otherwise returns false.
data types refers to the int, string, boolean etc. for example:
        		     					    		
$var1 = 101;

$var2 = "101";

// if statement executes when expression returns true.

if($var1 === $var2){
	echo "data type and value is matched";
}
else{
	echo "not matched";
}

output:

not matched
        		     					    		
        		     					    	
in the above example the output on the screen is " not matched " why ?
the $var1 value is 10 (data type is int - without quote) and the $var2 value is "10" (data type is string - with quotes.)
the value on both side is equal but their data type is different. so it returns false then else block executes.


== vs === in php


== is an equal operator in php which is used to compares both side values are equal.
whereas === is an identical operator in php which returns true only if both side variable values are equal and their corresponding type is also equal.


Not Equal Operator | != in php


in php != operator is used to check two variables value should not be equal.
it return true when both side variables value is not matched or equal. otherwise it returns false if both side values are equal.
for example:
        		     					    		
$x = 50;

$y = 51;

// if statement executes when expression returns true.

if($x != $y){
	echo "values not matched";
	return 1;
}
else{
	echo "values matched";
	return 0;
}

output:

values not matched
        		     					    		
        		     					    	
in the above example $x value 50 is not equal to $y value 51
which fulfills the if conditional statement and the block statement is executed.


Not Identical Operator | !== in php


in php !== operator is used to check two variables value and their types are not equal.
it is just opposite of === operator.
it returns true when two variable value is equal but their data type is not equal or matched. otherwise returns false.
data types refers to the int, string, boolean etc. for example:
        		     					    		
$x = 101;

$y = "101";

// if statement executes when expression returns true.

if($x !== $y){
	echo "not matched";
}
else{
	echo "matched";
}

output:

not matched
        		     					    		
        		     					    	
in the above example the output on the screen is " not matched " why ?
because the value in the variables on both sides is equal but their data type is not matched.
101 is int and "101" is string type.


Less than Operator | < in php


in php < operator is used to compare two variables whether first variable value is less than the other variable.
it returns true if left side variable value is less than the other variable. otherwise returns false
for example:
        		     					    		
$x = 7;

$y = 17;

if($x < $y){
	echo "$x is less than $y";
}
else{
	echo "$x is greter than $y";
}

output:

7 is less than 17
        		     					    		
        		     					    	


Greater than Operator | > in php


in php < operator is used to compare two variables whether first variable value is greater than the other variable.
it returns true if left side variable value is greater than the other variable. otherwise returns false
for example:
        		     					    		
$x = 51;

$y = 21;

if($x > $y){
	echo "$x is greater than $y";
}
else{
	echo "$x is less than $y";
}

output:

51 is greater than 21
        		     					    		
        		     					    	


Not Equal Operator | <> in php


in php <> operator is opposite of == operator
in == operator - the expression returns true when values on both sides are equal.
whereas in <> operator - the expression returns false when values on both sides are equal.
which means : -
=< it returns true if $x value is not equal to $y value.
=< and it returns true if $x value is not equal to $y value.
for example:
        		     					    		
$x = 21;

$y = 21;

// return false becuase both side values are equal.

if($x <> $y){
	echo "values are equal";
}
else{
	echo "values are not equal";
}

output:

values are not equal
        		     					    		
        		     					    	
in the above example the output is " values are not equal " why ?
because $x is equal to $y which is true.
but remember <> operator is opposite of == operator.
so it returns false and executes the else block statements.


Less than Or Equal to Operator | <= in php


in php <= operator is used to compare two variables based one two conditions which are :
condition 1 - it checks, if the first variable is less than the other variable in the expression.
condition 2 - it checks, if the first variable value is equal to the other variable value.

it returns true if any one of the condition is true.
if both the conditions are false then it returns false.
for example:
        		     					    		
$x = 17;

$y = 17;

if($x <= $y){
	echo "$x is less than $y";
}
else{
	echo "$x is greter than $y";
}

output:

17 is less than Or equal to 17
        		     					    		
        		     					    	


Greater than Or Equal to Operator | >= in php


in php >= operator is used to compare two variables based one two conditions which are :
condition 1 - it checks, if the first variable is greater than the other variable in the expression.
condition 2 - it checks, if the first variable value is equal to the other variable value.

it returns true if any one of the condition is true.
if both the conditions are false then it returns false.
for example:
        		     					    		
$x = 51;

$y = 21;

if($x >= $y){
	echo "$x is greater than $y";
}
else{
	echo "$x is less than $y";
}

output:

51 is greater than 21