Created
December 18, 2017 07:06
-
-
Save sanikamal/3e6ce95a89be89f4341b885d2e5d54e5 to your computer and use it in GitHub Desktop.
PHP comparison operators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
PHP Comparison Operators | |
The PHP comparison operators are used to compare two values (number or string): | |
== Equal $x == $y Returns true if $x is equal to $y | |
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type | |
!= Not equal $x != $y Returns true if $x is not equal to $y | |
<> Not equal $x <> $y Returns true if $x is not equal to $y | |
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are | |
not of the same type | |
> Greater than $x > $y Returns true if $x is greater than $y | |
< Less than $x < $y Returns true if $x is less than $y | |
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y | |
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y | |
--> | |
<?php | |
$num1=10; | |
$num2=7; | |
$numStr='10'; | |
//always true | |
if(true){ | |
echo 'I am always true'.'<br>'; | |
} | |
//always false | |
if(false){ | |
echo 'I am true'.'<br>'; | |
}else{ | |
echo 'I am always false'.'<br>'; | |
} | |
//return true if $num1 is equal to $numStr | |
if($num1==$numStr){ | |
echo 'we are same'.'<br>'; | |
} | |
//return true if $num1 is identical to $numStr and same type | |
if($num1===$numStr){ | |
echo 'we are identical'.'<br>'; | |
}else{ | |
echo 'We are not identical'.'<br>'; | |
} | |
//return true if $num1 is not equal to $num2 | |
if($num1!=$num2){ | |
echo 'we are not same'.'<br>'; | |
} | |
// same as != | |
if($num1<>$num2){ | |
echo 'we are not same'.'<br>'; | |
} | |
if($num1!==$numStr){ | |
echo 'we are not identical'.'<br>'; | |
} | |
//return true if $num1 is greater than $num2 | |
if($num1>$num2){ | |
echo $num1.' is greater than '.$num2.'<br>'; | |
}else{ | |
echo $num2.' is greater than '.$num1.'<br>'; | |
} | |
//return true if $num1 is less than $num2 | |
if($num1<$num2){ | |
echo $num1.' is less than '.$num2.'<br>'; | |
}else{ | |
echo $num2.' is less than '.$num1.'<br>'; | |
} | |
//return true if $num1 is greater than or equal to $num2 | |
if($num1>=$num2){ | |
echo $num1.' is greater than or equal to'.$num2.'<br>'; | |
}else{ | |
echo $num2.' is greater than or equal to'.$num1.'<br>'; | |
} | |
//return true if $num1 is less than or equal to $num2 | |
if($num1<$num2){ | |
echo $num1.' is less than or equal to '.$num2.'<br>'; | |
}else{ | |
echo $num2.' is less than or equal to '.$num1.'<br>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment