0
0
PHPprogramming~20 mins

Common validation patterns in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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';
}
?>
AValid email
BInvalid email
CSyntax error
DNo output
Attempts:
2 left
💡 Hint
filter_var with FILTER_VALIDATE_EMAIL returns the email if valid, false otherwise.
Predict Output
intermediate
2: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';
}
?>
AAll digits
BNot all digits
CWarning: ctype_digit expects string
DNo output
Attempts:
2 left
💡 Hint
ctype_digit returns true if all characters are digits.
🔧 Debug
advanced
2: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';
}
?>
AWarning: filter_var expects parameter 2 to be int
BInvalid URL
CValid URL
DSyntax error
Attempts:
2 left
💡 Hint
FILTER_VALIDATE_URL checks if the string is a valid URL format.
Predict Output
advanced
2: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';
}
?>
ANo output
BPassword is strong
CWarning: strlen expects parameter 1 to be string
DPassword too short
Attempts:
2 left
💡 Hint
strlen returns the length of the string.
🧠 Conceptual
expert
3: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?
AThe string '5' because filter_var always returns strings
BThe integer 5 because it is within the range 1 to 10
CNull because the options array is ignored
DBoolean false because filter_var does not accept options with FILTER_VALIDATE_INT
Attempts:
2 left
💡 Hint
FILTER_VALIDATE_INT accepts an options array to specify min and max range.