Skip to content

PHP Tutorial Operators

Logical Operators

Lesson 51 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Logical operators combine several conditions into one answer.

OperatorMeaningTrue when…
&&ANDboth sides are true
||ORat least one side is true
!NOTthe value is false (it flips the result)
xorExclusive ORexactly one side is true
index.php
PHP
<?php
$age = 25;
$hasTicket = true;

var_dump($age >= 18 && $hasTicket);
var_dump($age < 18 || $hasTicket);
var_dump(!$hasTicket);
Output
OUTPUT
bool(true)
bool(true)
bool(false)

Short-circuit evaluation

PHP stops as soon as it knows the answer. With &&, if the left side is false, the right side is never run. With ||, if the left side is true, the right side is skipped.

index.php
PHP
<?php
function check(string $label): bool
{
    echo "checking $label\n";
    return true;
}

false && check("A");
true || check("B");
true && check("C");
Output
OUTPUT
checking C

Only C ran. This is useful: you can guard a risky check with a safe one.

index.php
PHP
<?php
$user = null;

if ($user !== null && $user["active"]) {
    echo "Active user";
} else {
    echo "No active user";
}
Output
OUTPUT
No active user

If PHP evaluated the second half with $user set to null, it would complain. The first half protects it.

&& vs and

PHP also has the words and and or. They do the same job, but they bind more loosely than =, which gives a trap.

index.php
PHP
<?php
$result = true and false;   // read as: ($result = true) and false
var_dump($result);

$result = (true and false);
var_dump($result);
Output
OUTPUT
bool(true)
bool(false)

In the first line, PHP assigns true first, and only then applies and. So $result is true.

warning
Use && and
| in conditions | The word forms and and or have the lowest precedence, so they can behave unexpectedly. Stick to && and ||.

Exercise: can they ride?

A ride needs height of at least 120 cm and an adult with them, or an age of 14 or more. Write the condition for $height, $hasAdult and $age.

Show a possible solution
index.php
PHP
<?php
$height = 130;
$hasAdult = false;
$age = 15;

$canRide = ($height >= 120 && $hasAdult) || $age >= 14;

var_dump($canRide);
Output
OUTPUT
bool(true)