Bird
Raised Fist0
PHPprogramming~20 mins

Common validation patterns in PHP - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

Which PHP function is commonly used to validate if a string is a valid email address?

easy
A. preg_match with '/^\d+$/'
B. filter_var with FILTER_VALIDATE_EMAIL
C. strlen to check length
D. strtolower to convert to lowercase

Solution

  1. Step 1: Understand email validation in PHP

    PHP provides a built-in filter called FILTER_VALIDATE_EMAIL to check if a string is a valid email.
  2. Step 2: Identify the correct function usage

    filter_var with FILTER_VALIDATE_EMAIL returns the email if valid, or false if invalid.
  3. Final Answer:

    filter_var with FILTER_VALIDATE_EMAIL -> Option B
  4. Quick Check:

    Email validation = filter_var with FILTER_VALIDATE_EMAIL [OK]
Hint: Use filter_var with FILTER_VALIDATE_EMAIL for email checks [OK]
Common Mistakes:
  • Using preg_match with wrong regex for email
  • Checking length instead of format
  • Using string functions unrelated to validation
2.

Which of the following is the correct syntax to validate a URL using filter_var in PHP?

?
easy
A. filter_var($url, FILTER_VALIDATE_INT);
B. filter_var($url, FILTER_VALIDATE_EMAIL);
C. filter_var($url, FILTER_VALIDATE_URL);
D. filter_var($url, FILTER_VALIDATE_BOOLEAN);

Solution

  1. Step 1: Identify the filter for URL validation

    FILTER_VALIDATE_URL is the correct filter to check if a string is a valid URL.
  2. Step 2: Confirm correct function call

    filter_var takes the variable and the filter constant as parameters.
  3. Final Answer:

    filter_var($url, FILTER_VALIDATE_URL); -> Option C
  4. Quick Check:

    URL validation = filter_var with FILTER_VALIDATE_URL [OK]
Hint: Use FILTER_VALIDATE_URL with filter_var for URL validation [OK]
Common Mistakes:
  • Using FILTER_VALIDATE_EMAIL for URLs
  • Using integer or boolean filters incorrectly
  • Missing the filter parameter
3.

What will be the output of the following PHP code?

$phone = '123-456-7890';
if (preg_match('/^\d{3}-\d{3}-\d{4}$/', $phone)) {
    echo 'Valid phone';
} else {
    echo 'Invalid phone';
}
medium
A. Syntax error
B. Invalid phone
C. No output
D. Valid phone

Solution

  1. Step 1: Analyze the regex pattern

    The pattern /^\d{3}-\d{3}-\d{4}$/ matches exactly 3 digits, a dash, 3 digits, a dash, then 4 digits.
  2. Step 2: Check the input string against the pattern

    The string '123-456-7890' matches this pattern exactly.
  3. Final Answer:

    Valid phone -> Option D
  4. Quick Check:

    Regex matches phone format = Valid phone [OK]
Hint: Match exact pattern with preg_match regex anchors ^ and $ [OK]
Common Mistakes:
  • Forgetting anchors ^ and $ in regex
  • Using wrong quantifiers in regex
  • Not escaping special characters
4.

Identify the error in this PHP code snippet for validating an integer:

$num = '123a';
if (filter_var($num, FILTER_VALIDATE_INT)) {
    echo 'Valid integer';
} else {
    echo 'Invalid integer';
}
medium
A. filter_var returns false for '123a', so output is 'Invalid integer'
B. filter_var should use FILTER_VALIDATE_NUMBER instead
C. The variable $num should be cast to int before validation
D. filter_var does not validate integers

Solution

  1. Step 1: Understand filter_var with FILTER_VALIDATE_INT

    This filter returns the integer value if valid, or false if invalid.
  2. Step 2: Check input '123a'

    The string '123a' is not a valid integer, so filter_var returns false.
  3. Final Answer:

    filter_var returns false for '123a', so output is 'Invalid integer' -> Option A
  4. Quick Check:

    Invalid integer string returns false = 'Invalid integer' output [OK]
Hint: filter_var returns false if input is not a pure integer string [OK]
Common Mistakes:
  • Using FILTER_VALIDATE_NUMBER which does not exist
  • Casting before validation causing wrong results
  • Assuming filter_var always returns boolean
5.

You want to validate a username in PHP that must start with a letter, can contain letters, numbers, underscores, and be 5 to 15 characters long. Which preg_match pattern correctly validates this?

hard
A. /^[a-zA-Z][a-zA-Z0-9_]{4,14}$/
B. /^[0-9][a-zA-Z0-9_]{4,14}$/
C. /^[a-zA-Z0-9_]{5,15}$/
D. /^[a-zA-Z]{5,15}$/

Solution

  1. Step 1: Understand username requirements

    Username must start with a letter, then 4 to 14 characters of letters, digits, or underscores, total length 5-15.
  2. Step 2: Analyze each pattern

    /^[a-zA-Z][a-zA-Z0-9_]{4,14}$/ matches a letter at start, then 4-14 allowed chars, total 5-15 length. Others fail start or length rules.
  3. Final Answer:

    /^[a-zA-Z][a-zA-Z0-9_]{4,14}$/ -> Option A
  4. Quick Check:

    Start letter + allowed chars + length 5-15 = /^[a-zA-Z][a-zA-Z0-9_]{4,14}$/ [OK]
Hint: Start with letter, then allowed chars with correct length in regex [OK]
Common Mistakes:
  • Allowing digits as first character
  • Not enforcing length limits
  • Missing anchors ^ and $
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard