Check If Two Variable Not Equal In PHP
Code snippet for how to Check If Two Variable Not Equal In PHP with sample and detail explanation
Equal and unequal comparison operators allow us to evaluate variables and determine if they meet specific criteria. In PHP, we can easily check if two variables are not equal, providing valuable functionality for many different situations.
Code snippet
$firstVariable = 5;
$secondVariable = 10;
if($firstVariable != $secondVariable){
echo "The variables are not equal.";
}
else {
echo "The variables are equal.";
}
Code Explanation
In the given code, we have two variables: $firstVariable
set to 5 and $secondVariable
set to 10. PHP’s !=
operator is used to test whether the variables are not equal to each other.
The line if($firstVariable != $secondVariable)
translates to “if the first variable is not equal to the second variable.” If this condition is true, it will execute the statement echo "The variables are not equal."
.
On the other hand, if the !=
condition is false (meaning the two values are actually equal), the program will execute the else
statement and print out echo "The variables are equal."
. In our specific example, the statement ‘The variables are not equal.’ is printed because 5 is not equal to 10.
Remember that !=
only considers value for comparison, not the variable type. If you want to consider the variable type in comparison too, you should use the !==
operator. This is a broader topic known as ‘strict comparison’.