0
0
PHPprogramming~10 mins

Nested conditional 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 a 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 '<' will check for negative numbers instead.
2fill in blank
medium

Complete the code to check if a number is zero.

PHP
<?php
$number = 0;
if ($number [1] 0) {
    echo "Number is zero";
}
?>
Drag options to blanks, or click blank then click option'
A<
B!=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' will check if number is not zero.
3fill in blank
hard

Fix the error in the nested if statement to check if a number is negative.

PHP
<?php
$number = -5;
if ($number < 0) {
    if ($number [1] -10) {
        echo "Number is between -10 and 0";
    }
}
?>
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will check if number is less than -10, which is outside the range.
4fill in blank
hard

Fill both blanks to complete the nested condition that checks if a number is positive and even.

PHP
<?php
$number = 8;
if ($number [1] 0) {
    if ($number % 2 [2] 0) {
        echo "Number is positive and even";
    }
}
?>
Drag options to blanks, or click blank then click option'
A>
B==
C!=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' for positivity or '!=' for evenness will cause wrong results.
5fill in blank
hard

Fill all three blanks to complete the nested condition that checks if a number is negative, odd, and less than -5.

PHP
<?php
$number = -7;
if ($number [1] 0) {
    if ($number % 2 [2] 0) {
        if ($number [3] -5) {
            echo "Number is negative, odd, and less than -5";
        }
    }
}
?>
Drag options to blanks, or click blank then click option'
A<
B!=
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' for negativity or less than -5 check.