PHP not equal (With Examples)

A PHP not equal is a comparison operator which is represented by a symbol ( != or <>).

This is always used if you want to compare a data type of two given (values) in any case of whether the two values were equal or not, that’s the time that we need to use the not equal operator.

In this article, we will discuss PHP not equal and why it is important to learn and understand. This article is a continuation of the previous topic, entitled Types of Operators.

What is PHP not equal?

In PHP, not equal is used to compare two given values of data types. This is a comparison operator represented by the symbol ( != or <>).

In addition, this not-equal operator returns true once the data type of the two given values is the same. Even if the value in the variables stored is not the same.

And this not-equal operator will return false once the data type of the two given values were not the same even though the value of the two variables stored are the same.

Syntax:

$variable1 != $variable2;
$variable1 <> $variable2;

In order for us to know more about PHP equal I will provide an advanced example for you to use in your future development.

Comparison operators

ExampleNameResult
$a == $bEqualtrue if $a is equal to $b after type juggling.
$a === $bIdenticaltrue if $a is equal to $b, and they are of the same type.
$a != $bNot equaltrue if $a is not equal to $b after type juggling.
$a <> $bNot equaltrue if $a is not equal to $b after type juggling.
$a !== $bNot identicaltrue if $a is not equal to $b, or they are not of the same type.
$a < $bLess thantrue if $a is strictly less than $b.
$a > $bGreater thantrue if $a is strictly greater than $b.
$a <= $bLess than or equal totrue if $a is less than or equal to $b.
$a >= $bGreater than or equal totrue if $a is greater than or equal to $b.
$a <=> $bSpaceshipAn int less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively.

The following programs below demonstrate a working program that uses an equal operator to compare a data type of the given two values and return the output.

First example:

<?php

//an integer value is stored in a variable called variable1
$variable1 = 10;
echo "The value stored in the first variable is: $variable1"."\n";
//a string value is stored in a variable called variable2
$variable2 = "10";
echo "The value stored in the second variable is: $variable2"."\n";
//not equal operator is used to compare the data type of the given two variables and return the result
echo "The result returned after using not equal operator is: ";
var_dump($variable1 != $variable2);

?>

Output:

The value stored in the first variable is: 10
The value stored in the second variable is: 10
The result returned after using not equal operator is: bool(false)

Second example:

<?php
//an integer value is stored in a variable called variable1
$variable1 = "Glenn Magada Azuelo";
echo "The value stored in the first variable is: $variable1"."\n";
//a string value is stored in a variable called variable2
$variable2 = "A writer and a computer programmer";
echo "The value stored in the second variable is: $variable2"."\n";
//not equal operator is used to compare the data type of the given two variables and return the result
echo "The result returned after using not equal operator is: ";
var_dump($variable1 != $variable2);
?>

Output:

The value stored in the first variable is: Glenn Magada Azuelo
The value stored in the second variable is: A writer and a computer programmer
The result returned after using not equal operator is: bool(true)

Third example:

<?php
//an integer value is stored in a variable called variable1
$variable1 = 26.97;
echo "The value stored in the first variable is: $variable1"."\n";
//a string value is stored in a variable called variable2
$variable2 = 16.664455;
echo "The value stored in the second variable is: $variable2"."\n";
//not equal operator is used to compare the data type of the given two variables and return the result
echo "The result returned after using not equal operator is: ";
var_dump($variable1 != $variable2);
?>

Output:

The value stored in the first variable is: 26.97
The value stored in the second variable is: 16.664455
The result returned after using not equal operator is: bool(true)

Fourth example:

<?php
//an integer value is stored in a variable called variable1
$variable1 = "ITSOURCECODE.COM";
echo "The value stored in the first variable is: $variable1"."\n";
//a string value is stored in a variable called variable2
$variable2 = "SOURCECODEHERO.COM";
echo "The value stored in the second variable is: $variable2"."\n";
//not equal operator is used to compare the data type of the given two variables and return the result
echo "The result returned after using not equal operator is: ";
var_dump($variable1 != $variable2);
?>

Output:

The value stored in the first variable is: ITSOURCECODE.COM
The value stored in the second variable is: SOURCECODEHERO.COM
The result returned after using not equal operator is: bool(true)

How do I check if two strings are not equal in PHP?

In PHP, the strcmp() is a built-in function used to compare two strings.

In addition, this function is very case-sensitive, and the small and capital cases will be treated differently, during the comparison stage.

Conclusion

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials that could help you greatly.

Leave a Comment