0
0
PHPprogramming~10 mins

Logical operators in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if both conditions are true using the AND operator.

PHP
<?php
$age = 20;
$hasID = true;
if ($age >= 18 [1] $hasID) {
    echo "Access granted.";
} else {
    echo "Access denied.";
}
?>
Drag options to blanks, or click blank then click option'
Aand
B&&
C||
Dor
Attempts:
3 left
💡 Hint
Common Mistakes
Using || which means OR, not AND.
Using 'and' keyword which has lower precedence and can cause unexpected results.
2fill in blank
medium

Complete the code to check if at least one condition is true using the OR operator.

PHP
<?php
$isWeekend = false;
$isHoliday = true;
if ($isWeekend [1] $isHoliday) {
    echo "You can relax today.";
} else {
    echo "You have to work.";
}
?>
Drag options to blanks, or click blank then click option'
Axor
B&&
Cand
D||
Attempts:
3 left
💡 Hint
Common Mistakes
Using && which requires both conditions to be true.
Using xor which returns true only if exactly one condition is true.
3fill in blank
hard

Fix the error in the code to correctly negate the condition using the NOT operator.

PHP
<?php
$isRaining = false;
if ([1]$isRaining) {
    echo "Take an umbrella.";
} else {
    echo "No umbrella needed.";
}
?>
Drag options to blanks, or click blank then click option'
A!
Bnot
C~
D!!
Attempts:
3 left
💡 Hint
Common Mistakes
Using ~ which is bitwise NOT, not logical NOT.
Using 'not ' which is valid but less common and can cause precedence issues.
4fill in blank
hard

Fill in the blank to create a condition that is true only if exactly one of the two variables is true.

PHP
<?php
$hasTicket = true;
$hasInvitation = false;
if ($hasTicket [1] $hasInvitation) {
    echo "Entry allowed.";
} else {
    echo "Entry denied.";
}
?>
Drag options to blanks, or click blank then click option'
A&&
B||
Cxor
Dand
Attempts:
3 left
💡 Hint
Common Mistakes
Using && which requires both to be true.
Using || which allows both to be true.
5fill in blank
hard

Fill all three blanks to create a complex condition that checks if a user is an adult and either has a membership or a guest pass.

PHP
<?php
$age = 22; $hasMembership = false;
$hasGuestPass = true;
if ($age >= [1] [3] ($hasMembership [2] $hasGuestPass)) {
    echo "Access granted.";
} else {
    echo "Access denied.";
}
?>
Drag options to blanks, or click blank then click option'
A18
B||
C&&
D21
Attempts:
3 left
💡 Hint
Common Mistakes
Using 21 instead of 18 for adult age.
Mixing up && and || operators.