Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP if Operators

Comparison Operators

If statements typically include conditions that compare two values.

Example

Verify if $t is equal to 14:

$t = 14;
if ($t == 14) {
 echo "Have a good day!";
}

To compare two values, we use comparison operators.

Here are the PHP comparison operators used in if statements:

Operator

Name

Result

==

Equal

Returns true if the values are identical

===

Identical

Returns true if the values and their data types are identical

!=

Not equal

Returns true if the values are different

<> 

Not equal

Returns true if the values are unequal

!==

Not identical

Returns true if the values or data types differ

Greater than

Returns true if the first value is greater than the second value.

Less than

Returns true if the first value is less than the second value.

>=

Greater than or equal to

Returns true if the first value is greater than or equal to the second value.

<=

Less than or equal to

Returns true if the first value is less than or equal to the second value.

Logical Operators

To evaluate multiple conditions, we can use logical operators, such as the && operator:

Example

Check if $a is greater than $b and if $a is less than $c.

$a = 200;
$b = 33;
$c = 500;
if ($a > $b && $a < $c ) {
 echo "Both conditions are true";
}

Here are the PHP logical operators that can be used in if statements:

Operator

Name

Description

and

And

True if both conditions are true

&&

And

True if both conditions are true

or

Or

True if either condition is true

||

Or

True if either condition is true

xor

Xor

True if either condition is true, but not both

!

Not

True if condition is not true

We can compare multiple conditions within a single if statement.

Example

Check if $a is one of the values: 2, 3, 4, 5, 6, or 7.

$a = 5;
if ($a == 2 || $a == 3 || $a == 4 || $a == 5 || $a == 6 || $a == 7) {
 echo "$a is a number between 2 and 7";
}