0
0
PHPprogramming~10 mins

Isset, empty, and is_null behavior 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 variable $var is set.

PHP
<?php
$var = 5;
if ([1]($var)) {
    echo "Variable is set.";
} else {
    echo "Variable is not set.";
}
?>
Drag options to blanks, or click blank then click option'
Adefined
Bempty
Cisset
Dis_null
Attempts:
3 left
💡 Hint
Common Mistakes
Using empty() instead of isset() which also checks for empty values.
Using is_null() which only checks if a variable is null, but does not check if it is set.
2fill in blank
medium

Complete the code to check if the variable $var is empty.

PHP
<?php
$var = 0;
if ([1]($var)) {
    echo "Variable is empty.";
} else {
    echo "Variable is not empty.";
}
?>
Drag options to blanks, or click blank then click option'
Astrlen
Bisset
Cis_null
Dempty
Attempts:
3 left
💡 Hint
Common Mistakes
Using isset() which returns true even if the variable is 0 or empty string.
Using is_null() which only checks if the variable is null.
3fill in blank
hard

Fix the error in the code to correctly check if $var is null.

PHP
<?php
$var = null;
if ([1]($var)) {
    echo "Variable is null.";
} else {
    echo "Variable is not null.";
}
?>
Drag options to blanks, or click blank then click option'
Ais_null
Bempty
Cisset
Ddefined
Attempts:
3 left
💡 Hint
Common Mistakes
Using isset() which returns false if variable is null but does not check for null explicitly.
Using empty() which returns true for many values, not just null.
4fill in blank
hard

Fill both blanks to include only words with length greater than 3 in the associative array.

PHP
<?php
$words = ['apple', 'cat', 'dog', 'banana'];
$result = [];
foreach ($words as $word) {
    if ([1]($word) [2] 3) {
        $result[$word] = strlen($word);
    }
}
print_r($result);
?>
Drag options to blanks, or click blank then click option'
Astrlen
B<
C>
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which works for arrays, not strings.
Using '<' instead of '>' which would select shorter words.
5fill in blank
hard

Fill all three blanks to create an array filtering code that keeps only non-empty, set, and non-null values.

PHP
<?php
$values = [0, null, '', 'hello', false, 42];
$result = [];
foreach ($values as $val) {
    if (![1]($val) && [2]($val) && ![3]($val)) {
        $result[] = $val;
    }
}
print_r($result);
?>
Drag options to blanks, or click blank then click option'
Aempty
Bisset
Cis_null
Dis_string
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_null() directly in the condition without negation.
Using is_string() which is unrelated to emptiness or being set.