Challenge - 5 Problems
PHP Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code validating an email?
Consider the following PHP code that checks if an email is valid using filter_var. What will it output?
PHP
<?php $email = 'user@example.com'; if (filter_var($email, FILTER_VALIDATE_EMAIL)) { echo 'Valid email'; } else { echo 'Invalid email'; } ?>
Attempts:
2 left
💡 Hint
filter_var with FILTER_VALIDATE_EMAIL returns the email if valid, false otherwise.
✗ Incorrect
The email 'user@example.com' is a valid email format, so filter_var returns the email string, making the if condition true and printing 'Valid email'.
❓ Predict Output
intermediate2:00remaining
What does this PHP code output when validating a numeric string?
Given this PHP snippet that uses ctype_digit to check if a string contains only digits, what will it output?
PHP
<?php $input = '12345'; if (ctype_digit($input)) { echo 'All digits'; } else { echo 'Not all digits'; } ?>
Attempts:
2 left
💡 Hint
ctype_digit returns true if all characters are digits.
✗ Incorrect
The string '12345' contains only digits, so ctype_digit returns true and the code prints 'All digits'.
🔧 Debug
advanced2:00remaining
What error does this PHP code raise when validating a URL?
Examine this PHP code that attempts to validate a URL. What error or output will it produce?
PHP
<?php $url = 'http://example.com'; if (filter_var($url, FILTER_VALIDATE_URL)) { echo 'Valid URL'; } else { echo 'Invalid URL'; } ?>
Attempts:
2 left
💡 Hint
FILTER_VALIDATE_URL checks if the string is a valid URL format.
✗ Incorrect
The string 'http://example.com' is a valid URL, so filter_var returns the URL string, making the if condition true and printing 'Valid URL'.
❓ Predict Output
advanced2:00remaining
What is the output of this PHP code validating a password length?
This PHP code checks if a password length is at least 8 characters. What will it output?
PHP
<?php $password = 'pass123'; if (strlen($password) >= 8) { echo 'Password is strong'; } else { echo 'Password too short'; } ?>
Attempts:
2 left
💡 Hint
strlen returns the length of the string.
✗ Incorrect
The password 'pass123' has 7 characters, which is less than 8, so the code prints 'Password too short'.
🧠 Conceptual
expert3:00remaining
Which option correctly describes the behavior of PHP's filter_var with FILTER_VALIDATE_INT and options?
Consider the following PHP code snippet:
$options = ['options' => ['min_range' => 1, 'max_range' => 10]]; $value = 5; $result = filter_var($value, FILTER_VALIDATE_INT, $options);What does $result contain after execution?
Attempts:
2 left
💡 Hint
FILTER_VALIDATE_INT accepts an options array to specify min and max range.
✗ Incorrect
filter_var returns the integer value if it is within the specified range. Since 5 is between 1 and 10, $result is 5 (int).