HomeLinuxUse Not Equal Operator in PHP with Examples

Use Not Equal Operator in PHP with Examples


Comparability operators are key options in PHP for evaluating expressions and making selections primarily based on their outcomes. One such operator is the not equal operator, which is used to match two values and return a Boolean outcome indicating whether or not they aren’t equal. PHP builders can use this operator to jot down environment friendly and efficient code that may deal with a variety of situations.

This text will undergo the not equal operator in PHP, its syntax, and use it with examples.

Use Not Equal Operator in PHP with Examples

The not equal operator is a comparability operator that returns a Boolean worth of true if the 2 values being in contrast usually are not equal and false in any other case.

The desk under presents the utilization of not-equal operators in PHP with corresponding symbols, an instance, and the ensuing output.

Operator Image

Instance

End result

!= $x != $y Returns true if x isn’t equal to y
<>  $x <> $y Returns true if x isn’t equal to y

The not-equal operator returns the worth true if the left-hand operand isn’t equal to the right-hand operand in any other case the output is fake.

Word: The != and <> operators can be utilized interchangeably in PHP.

Instance 1

Within the instance under, a comparability has been made between two variables, a and b utilizing the not-equal operator.

<?php
$a = 10;
$b = 10;
var_dump($a != $b);
?>

 
Or:

<?php
$a = 10;
$b = 10;
var_dump($a <> $b);
?>

 
Since they’re equal, the output shall be a boolean worth of “false”.

Output

 

Instance 2

Within the below-given code, the not equal operator is used to test the worth of the variable a is the same as 10 or not, if the worth isn’t equal to 10 and the situation is true the code contained in the block shall be executed in any other case the primary assertion outdoors the block would be the output:

<?php
$a = 5;
if ($a != 10) {
    echo “a isn’t equal to 10”;
}
?>

 
For the reason that worth of a isn’t equal to 10, the output “a isn’t equal to 10” shall be displayed within the console.

Output

 

Instance 3

The not equal operator in PHP may also be utilized in mixture with the whereas loop, as demonstrated within the following instance.

<?php
$i = 1;
whereas($i != 5) {
  echo “The worth of i is: “ . $i . n;
  $i++;
}
?>

 
The above code prints the worth of a variable i from 1 to 4. The whereas loop terminates when the worth of i isn’t equal to five, which is checked utilizing the not equal operator within the situation.

Output

 

Conclusion

The not equal operator is a crucial PHP characteristic that permits builders to match two values and return a Boolean outcome indicating their inequality. This text illustrates its implementation and utilization, highlighting its syntax together with examples to assist programmers perceive and apply it of their applications.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments