PHP Not Equal

!= and <> are the Not Equal comparison operators of PHP. While == checks two variables are equal or not. If the condition is met, return TRUE, otherwise FALSE. These operators are not type sensitive.

<?PHP
	$a = 3;
	if ($a != 4) {...}  //True
	if ($a <> 4) {...}  //True
	if ($a == "3") {...} //True
	
	$a = array("2","3.4","5.342","0","3","-5","6.3");
	$b = array(2,3.4,5.342,0,3,-5,6.3);
	if ($a == $b) echo "True"; else echo "False"; //True
?>

The operators can be used to compare arrays.

<?PHP
	$a = array("2","3.4","5.342","0","3","-5","6.3");
	$b = array(2,3.4,5.342,0,3,-5,6.3);
	if ($a == $b) echo "True"; else echo "False"; //True
?>

=== checks whether two variables are equal and of the same type, as identical. !== is the Not Identical signs of PHP.

<?PHP
	$a = 3;
	if ($a == "3") {...}  //True
	if ($a === "3") {...}  //False
	if ($a !== "3") {...}  //True	
?>

is_null(x) checks x is NULL or not.

<?PHP
  $a = NULL;
  if ($a == NULL) echo "True";  //True
  if ($a === NULL) echo "True"; //True
  if (is_null($a)) echo "True"; //True

  $a ='';
  if ($a == NULL) echo "True"; else echo "False1"; //True
  if ($a === NULL) echo "True"; else echo "False2"; //False
  if (is_null($a)) echo "True"; else echo "False3";  //False
?>

When the operator is not type sensitive, 0, FALSE, empty string will be treated as equal to each other.

<?PHP
   $a="";$b=0; $c=FALSE;
   if ($a == $b) echo "True"; else echo "False"; //True
   if ($a === $b) echo "True"; else echo "False"; //False
   if ($c == $b) echo "True"; else echo "False"; //True
   if ($c == $b) echo "True"; else echo "False"; //False
   
    $a = NULL;
	$b = 4/0;
    if ($a == $b) echo "True"; else echo "False";  //True   
?>

<=> spaceship operator compares two integer or float numbers. If the 1st is equal to the 2nd, return 0; else if less than, return -1, else if greater than, return 1. This operator is only supported in PHP 7 or above.

<?PHP
    echo 2.1 <=> 5;  //-1
	echo 2.1 <=> 2.1;  //0
	echo 9 <=> 4;   //1
?>

Following is the list of all comparison operators in PHP.

Operator
Description
==
equal, not type sensitive
===
identical, type sensitive
!=
not equal, not type sensitive
!==<>
not identical, type sensitive
<
less than
>
greater than
<=
less than or equal to
>=
greater than or equal to
<=>
return 0 if equal, -1 if less than or 1 if greater than



:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • regular expr. • str_replace
 • strcmp • strpos
 • strrev • strrpos
 • strtr • substr
 • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expr. • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2024  | Terms of Use | Privacy | Home