0
0
PHPprogramming~10 mins

Elseif ladder execution 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 the number is positive.

PHP
<?php
$number = 10;
if ($number [1] 0) {
    echo "Positive number";
}
?>
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will check for negative numbers.
Using '==' checks only if number equals zero.
2fill in blank
medium

Complete the elseif condition to check if the number is zero.

PHP
<?php
$number = 0;
if ($number > 0) {
    echo "Positive number";
} elseif ($number [1] 0) {
    echo "Zero";
}
?>
Drag options to blanks, or click blank then click option'
A==
B<
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' checks if number is not zero, which is incorrect here.
Using '<' or '>' checks for negative or positive numbers, not zero.
3fill in blank
hard

Fix the error in the elseif condition to check if the number is negative.

PHP
<?php
$number = -5;
if ($number > 0) {
    echo "Positive number";
} elseif ($number [1] 0) {
    echo "Negative number";
}
?>
Drag options to blanks, or click blank then click option'
A>
B!=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' checks for positive numbers, not negative.
Using '==' checks if number equals zero, not negative.
4fill in blank
hard

Fill both blanks to complete the elseif ladder that prints the correct message based on the number.

PHP
<?php
$number = 0;
if ($number [1] 0) {
    echo "Positive number";
} elseif ($number [2] 0) {
    echo "Negative number";
} else {
    echo "Zero";
}
?>
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the operators will cause wrong messages.
Using '==' in the first two conditions misses positive or negative checks.
5fill in blank
hard

Fill all three blanks to create an elseif ladder that prints the correct message for positive, negative, or zero values.

PHP
<?php
$number = -3;
if ($number [1] 0) {
    echo "Positive number";
} elseif ($number [2] 0) {
    echo "Negative number";
} elseif ($number [3] 0) {
    echo "Zero";
}
?>
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' for zero check causes wrong output.
Mixing up the operators leads to incorrect messages.