[Explained]: What is the difference between != and !== in PHP?
  John Mwaniki /   02 Oct 2021

[Explained]: What is the difference between != and !== in PHP?

In a previous article, I covered the differences between =, == and === which causes much confusion to most new programmers.

I have since then had someone ask me the difference between != and !== which is pretty closely similar to the earlier three. Bearing in mind that I too faced a challenge differentiating the two and would often use them interchangeably in my earlier days of programming, I thought there could be many people out there who would benefit if I wrote about it.

In this article, I will delve into detail and explain the differences between != and !==, giving numerous code examples for demonstration.

If you are very new to programming, you may be asking yourself what the heck the two are. Or what on earth is an exclamation mark doing before an equal sign.

The two symbols are referred to as operators in programming. An operator is a sign/symbol that gives instruction to the compiler or the interpreter to perform a specific operation/action to an operand(s) or variable(s).

This could be an arithmetic operation, logical operation, comparison of two values, assigning a value to a variable or constant, etc.

Examples of other commonly used operators include +, -, /, *, <, >, etc

Difference Between != and !== in PHP

One thing you may notice of the two is that they both start with an exclamation mark (!).

In programming, an exclamation when used as an operator means not and is used to negate whatever is placed before. For instance, if you say "!I love programming", the programmers will interpret it as "I don't love programming".

1. != Operator

Since "=" means "equal to", "!=" means "not equal to". It is a comparison operator, referred to as not equal, and is used to compare if two operands have equal values.

Its check is that the two should not be equal. It returns true if the values of the two operands are not equal, and returns false if they happen to be equal.

Example 1

<?php
if(10 != 10){
 echo "Values not equal";
}
else{
  echo "Values equal";
}
?>

Output:

False: Values equal

In the example above, values on both sides of the operator are equal so the output is false.

Example 2

<?php
if(10 != "10"){
 echo "Values not equal";
}
else{
  echo "Values equal";
}
?>

Output:

False: Values equal

Still in the above example, values on both sides of the operator are equal and so the output is false.

Example 3

<?php
if(10 != 23){
 echo "Values not equal";
}
else{
  echo "Values equal";
}
?>

Output:

True: Values not equal

The value on the left(10) is not equal to the value on the right (23), thus the true outcome.

2. !== Operator

Like the above, this too is a comparison operator as it too compares values. It is referred to as not identical operator. It is the opposite of the identical (===) operator.

It's used to confirm that two values/operands are not identical. Identical means being equal in value and being of the same type.

It returns true if the value on the left is not equal to the value on the right, or they are not of the same type. It only returns false if the two values are equal and are of the same type.

Example 1

<?php
if(10 !== 10){
 echo "Values not identical";
}
else{
  echo "Values identical";
}
?>

Output:

False: Values identical

10 on the left and 10 on the right are both equal in value and are of the same type, integer. Thus they are identical. Since our check was to confirm that they are not identical it fails and returns false.

Example 2

<?php
if(10 !== "10"){
 echo "Values not identical";
}
else{
  echo "Values identical";
}
?>

Output:

True: Values not identical

10 on the left and 10 on the right are equal in value and but are of different types. The left one is an integer while the right one is a string(since it is enclosed in quotes ""). Therefore they are not identical. Since the operator is used to check/confirm that they are not identical, our check returns true.

Example 3

<?php
if(10 !== 2){
 echo "Values not identical";
}
else{
  echo "Values identical";
}
?>

Output:

True: Values not identical

From the example above, though the two values are integers, they are not equal and thus not identical. Hence true outcome.