PHP Comparison Operators

PHP Comparison operators is used to perform comparison operations. There are various types of comparison operators available to perform various types of comparison. Lets discuss in detail.

Equal Operator(==)

Equal operator (==) is used to compare two values, and if both values are same, then it returns true otherwise it returns false.

Example:

<?php

$num1=100;

$num2=100;

if($num1==$num2){

echo “Both numbers are same”;

}else{

echo “Both numbers are not same”;

}

?>

If you run above code, then the output would be, Both numbers are same. Here ‘if block’ compares $num1 value and $num2 value and executes first block if condition matches. Here both numbers are same, so it executed the first block. (If you are not aware of if else statement, then refer our PHP IF Else Statement Chapter)

Example:

<?php

$num1=100;

$num2=102;

if($num1==$num2){

echo “Both numbers are same”;

}else{

echo “Both numbers are not same”;

}

?>

If you run the above code, the output would be: Both numbers are not same. Because both numbers are not same.

The same way we compared numbers, we can compare, string values also.

<?php

$name1=”rakesh”;

$name2=”suresh”;

if($name1==$name2){

echo “both names are same”;

}else{

echo “both names are not same”;

}

?>

The output of the above code would be: both names are not same. Because $name1 and $name2 both are different.

<?php

$name1=”rakesh”;

$name2=”rakesh”;

if($name1==$name2){

echo “both names are same”;

}else{

echo “both names are not same”;

}

?>

The output of the above code would be: both names are same. Because $name1 and $name2 both are same.

Equal Same Type Operator or Identical operator (===)

Equal of same type operator (===) is used to compare values of two variables. But wait, we have already equal operator (==) for this. Then why do we need equal of same type operator(===). The following example would clarify.

<?php

$num1=5;

$num2=”5″;

if($num1==$num2){

 echo “both values are same”;

}else{

echo “both values are different”;

}

?>

Note: Here we have used equal operator (==). And also notice, $num1 value is 5 (its integer value). But value of $num2 is “5” (its string value).

Okay now guess, what would be the output of the above code.

You must be thinking, it would print, both values are different.

Now, run the above code and you would wonder to notice, it prints “both values are same”

Why this happened?

Because PHP Engine, understands the type of variable from its value. Here the value of $num2 is “5” which is string but PHP engine understands it as integer value.

The solution is to use equal same type operator (===).

It not only compares value of two variables but also checks both datatype.

<?php

$num1=5;

$num2=”5″;

if($num1===$num2){

 echo “both values are same”;

}else{

echo “both values are different”;

}

?>

Now see, in the above code, we have used equal same type operator.

The output of the above code would be “both values are different”

Not Equal Operator (!=,<>)

Not equal operator is used to check whether the values of two variables are different.

<?php

$num1=10;

$num2=15;

if($num1!=$num2){

 echo “both values are different”;

}else{

echo “both values are not different”;

}

?>

If we run the above code, it would give us: both values are different.

Note: != is known as not equal operator, and alternatively ‘<>’ can also be used for same purpose and it is also known as not equal operator.

<?php

$num1=100;

$num2=200;

if($num1<>$num2){

echo “both numbers are different”;

}else{

echo “both numbers are same”;

}

?>

The output of the above code would be “both numbers are different”

Not Equal Same Type Operator(!==)

Not Equal same type operator (!==) is used to compare values of two variables along with both datatypes.

We have already discussed in equal same type operator, why we need ‘equal same type operator(===)’

<?php

$num1=100;

$num2=”100″;

if($num1!==$num2){

echo “both values are different”;

}else{

echo “both values are same with same datatype”;

}

?>

The output of the above code would be: both values are different. Because $num1 value is 100 and it is type of integer. But $num2 is 100 but it is type of string.

Less than operator (<)

Less than operator is used to compare whether value of one variable is less than the value of another variable.

<?php

$num1=5000;

$num2=6000;

if($num1<$num2){

echo “Num1 is less than Num2”;

}else{

echo “Num1 is not less than Num2”;

}

?>

The output of the above code would be, Num1 is less than Num2.

Less than equal operator(<=)

Less than equal operator is used to compare whether the value of one variable is less than or equal to value of another variable.

<?php

$num1=5000;

$num2=5000;

if($num1<=$num2){

echo “Num1 is less than or equal to Num2”;

}else{

echo “Num1 is not less than or equal to Num2”;

}

?>

The output of the above code would be: Num1 is less than or equal to Num2.

<?php

$num1=4000;

$num2=5000;

if($num1<=$num2){

echo “Num1 is less than or equal to Num2”;

}else{

echo “Num1 is not less than or equal to Num2”;

}

?>

The output of the above code would be: Num1 is less than or equal to Num2.

Greater than operator (>)

Greater than operator is used to compare whether the value of one variable is greater than the value of another variable.

<?php

$num1=6000;

$num2=5000;

if($num1>$num2){

echo “Num1 is greater than  Num2”;

}else{

echo “Num1 is not greater than Num2”;

}

?>

The output of the above code would be: Num1 is greater than Num2

Greater than equal operator (>=)

Greater than equal operator is used to compare whether the value of one variable is greater than the value of another variable.

<?php

$num1=5000;

$num2=5000;

if($num1>=$num2){

echo “Num1 is greater than  or equal to Num2”;

}else{

echo “Num1 is not greater than or equal to Num2”;

}

?>

The output of the above code would be: Num1 is greater than or equal to Num2

<?php

$num1=7000;

$num2=5000;

if($num1>=$num2){

echo “Num1 is greater than  or equal to Num2”;

}else{

echo “Num1 is not greater than or equal to Num2”;

}

?>

The output of the above code too would be: Num1 is greater than or equal to Num2.