Check If Two Variable Is Not Equal In PHP
Code snippet for how to Check If Two Variable Is Not Equal In PHP with sample and detail explanation
In this article, we will be discussing one of the indispensable topics in PHP, which is how to check if two variables are not equal. PHP, being a dynamic and server-side scripting language, offers various ways to compare variables, and we’d be particularly focussing on the ‘not equal to’ comparison here.
Code snippet for ‘Not Equal To’
Below is a simple code snippet highlighting the usage of ‘not equal to’ in PHP.
<?php
$a = 5;
$b = 10;
if($a != $b) {
echo "The variables are not equal.";
} else {
echo "The variables are equal.";
}
?>
Code Explanation for ‘Not Equal To’
Let’s break down the above code snippet and understand step by step what each line implies.
-
$a = 5; $b = 10;
These are simple variable declarations where we assign the values 5 and 10 to variables
$a
and$b
respectively. -
if($a != $b) {
Here we use the ‘not equal to’ operator (
!=
) in PHP to compare the two variables$a
and$b
. The condition would return true if$a
is not equal to$b
. -
echo "The variables are not equal.";
If the condition in the ‘if’ statement is met i.e., if
$a
is not equal to$b
, the string “The variables are not equal.” is printed. -
} else { echo "The variables are equal."; }
The ‘else’ block will execute if the condition in the ‘if’ statement does not hold true i.e., if
$a
is equal to$b
, it would print the string “The variables are equal.”
And that’s it! By following these steps, you can easily check if two variables are not equal in PHP. Just remember to use the ‘not equal to’ operator (!=
) and you will be good to go.