Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a variable is set.
PHP
<?php if ([1]( $name )) { echo "Name is set."; } else { echo "Name is not set."; } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using empty() which returns true if the variable is empty, not just set.
Using is_null() which checks if variable is null but does not check if it is set.
✗ Incorrect
The isset function checks if a variable is set and is not null.
2fill in blank
mediumComplete the code to validate if a string contains only digits.
PHP
<?php if ([1]($input)) { echo "Input is numeric."; } else { echo "Input is not numeric."; } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_numeric() which allows decimal points and signs.
Using preg_match without a proper pattern.
✗ Incorrect
The ctype_digit function checks if all characters in the string are digits.
3fill in blank
hardFix the error in the code to validate an email address.
PHP
<?php $email = "user@example.com"; if (filter_var($email, [1])) { echo "Valid email."; } else { echo "Invalid email."; } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using FILTER_SANITIZE_EMAIL which only cleans the email but does not validate.
Using FILTER_VALIDATE_INT which is for integers.
✗ Incorrect
FILTER_VALIDATE_EMAIL is used with filter_var to check if the email is valid.
4fill in blank
hardFill both blanks to create an array of words longer than 3 characters.
PHP
<?php $words = ['cat', 'house', 'dog', 'elephant']; $longWords = array_filter($words, function($word) { return strlen($word) [1] [2]; }); print_r($longWords); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Using 5 instead of 3 changing the condition.
✗ Incorrect
The code filters words with length greater than 3 using strlen($word) > 3.
5fill in blank
hardFill all three blanks to create an associative array of words and their lengths for words longer than 4.
PHP
<?php $words = ['apple', 'bat', 'carrot', 'dog']; $lengths = array_filter(array_combine( array_map(function($word) { return [1]; }, $words), array_map(function($word) { return [2]; }, $words) ), function($len) { return $len [3] 4; }); print_r($lengths); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the filter condition.
Using wrong variable names in map functions.
✗ Incorrect
The code maps words as keys and their lengths as values, then filters lengths greater than 4.