Complete the code to check if a variable is set.
<?php if ([1]( $name )) { echo "Name is set."; } else { echo "Name is not set."; } ?>
The isset function checks if a variable is set and is not null.
Complete the code to validate if a string contains only digits.
<?php if ([1]($input)) { echo "Input is numeric."; } else { echo "Input is not numeric."; } ?>
The ctype_digit function checks if all characters in the string are digits.
Fix the error in the code to validate an email address.
<?php $email = "user@example.com"; if (filter_var($email, [1])) { echo "Valid email."; } else { echo "Invalid email."; } ?>
FILTER_VALIDATE_EMAIL is used with filter_var to check if the email is valid.
Fill both blanks to create an array of words longer than 3 characters.
<?php $words = ['cat', 'house', 'dog', 'elephant']; $longWords = array_filter($words, function($word) { return strlen($word) [1] [2]; }); print_r($longWords); ?>
The code filters words with length greater than 3 using strlen($word) > 3.
Fill all three blanks to create an associative array of words and their lengths for words longer than 4.
<?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); ?>
The code maps words as keys and their lengths as values, then filters lengths greater than 4.
