PHP if Operators

Back to ACP page

Table of Contents

PHP 11 Comparison Operators Equal - identical

To compare two values, we need to use a comparison operator. Here are the PHP comparison operators to use in if statements:

Operator Name Example Result
== Equal $x == $y True if $x is equal to $y
=== Identical $x === $y True if $x is equal to $y, and they are of the same type
> Greater than $x > $y True if $x is greater than $y
< Less than $x < $y True if $x is less than $y
>= Greater than or equal to $x >= $y True if $x is greater than or equal to $y
<= Less than or equal to $x <= $y True if $x is less than or equal to $y
<h2>if_Comparison_Operators_Equal_identical</h2> <h4>The == Equal Operator</h4> <p>Compare two variables to check if they have the same value.</p> <?php $x = 100; $y = 100; if ($x == $y) { echo "$x is equal to $y"; } ?> <h4>The === identical Operator</h4> <p>Compare two variables to check if they are identical.</p> <p>The identical operator (===) checks the value and the data type, unlike the equal operator (==) that checks only the value.</p> <?php $x = 100; $y = 100; if ($x === $y) { echo "$x is identical to $y"; } ?>

Example 11

Result View Example

PHP 12 Comparison Operators Not equal - NOT identical

Operator Name Example Result
!= Not equal $x != $y True if $x is not equal to $y
<> Not equal $x <> $y True if $x is not equal to $y
!== Not identical $x !== $y True if $x is not equal to $y, or they are not of the same type
<h2>if Comparison Operators Not equal - NOT identical</h2> <h4>The != Not equal Operator</h4> <p>Compare two variables and write a message if they don't have the same value.</p> <?php $x = 100; $y = 50; if ($x != $y) { echo "$x is not equal to $y"; } ?> <h4>The <> Not equal Operator</h1> <p>Compare two variables and write a message if they don't have the same value.</p> <?php $x = 100; $y = 50; if ($x <> $y) { echo "$x is not equal to $y"; } ?> <h4>The !== NOT identical Operator</h4> <p>Compare two variables and write a message if they are NOT identical.</p> <p>The not identical operator (!==) checks the value and the data type, unlike the not equal operator (!=) that checks only the value.</p> <?php $x = 100; $y = 50; if ($x !== $y) { echo "$x is not identical to $y"; } ?>

Example 12

Result View Example

PHP 13 Comparison Operators Greater than - Less than

Operator Name Example Result
> Greater than $x > $y True if $x is greater than $y
< Less than $x < $y True if $x is less than $y
<h2>if Comparison_Operators Greater than - Less than </h2> <h4>Greater than</h4> <?php $x = 100; $y = 50; if ($x > $y) { echo "$x is greater than $y"; } ?> <h4>Less than</h4> <?php $x = 100; $y = 50; if ($y < $x) { echo "$y is less than $x"; }

Example 13

Result View Example

Greater_than_or_equal_to_-_Less_than_or_equal_to

PHP 14 Comparison Operators Greater than or equal to - Less than or equal to

Operator Name Example Result
>= Greater than or equal to $x >= $y True if $x is greater than or equal to $y
<= Less than or equal to $x <= $y True if $x is less than or equal to $y
<h4>Greater than or equal to</h4> <?php $x = 100; $y = 100; if ($x >= $y) { echo "$x is greater than, or equal to $y"; } ?> <h4>Less than or equal to</h4> <?php $x = 100; $y = 100; if ($y <= $x) { echo "$y is less than, or equal to $x"; } ?>

Example 14

Result View Example

PHP 21 Logical Operators

Operator Name Example Result
and And $x and $y True if both $x and $y are true
&& And $x && $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
|| Or $x || $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
! Not !$x True if $x is not true
<h2>Logical Operators</h2> <p>To check more than one condition, we can use logical operators</p> <h4>and - && And</h4> <?php $x = 100; $y = 50; if ($x == 100 and $y == 50) { echo "Hello world! and 1"; } if ($x == 100 && $y == 50) { echo "Hello world! and 2"; } ?> <h4>or - || Or</h4> <?php $x = 100; $y = 50; if ($x == 100 or $y == 80) { echo "Hello world! or 1"; } if ($x == 100 || $y == 80) { echo "Hello world! or 2"; } ?> <h4>xor - Xor</h4> <?php $x = 100; $y = 50; if ($x == 100 xor $y == 80) { echo "Hello world! Xor"; } ?> <h4>! Not</h4> <?php $x = 100; if (!($x == 90)) { echo "Hello world! Not"; } ?>

Document

Document in project

You can Download PDF file.

Reference