Input validation is the process of checking user input to ensure it meets expected formats and rules before processing it. This helps prevent errors and security issues.
Click to reveal answer
beginner
How do you validate an email address in PHP?
Use the filter_var() function with FILTER_VALIDATE_EMAIL to check if a string is a valid email format.
Click to reveal answer
beginner
What PHP function helps validate if a string contains only letters and numbers?
ctype_alnum() checks if all characters in the string are letters or digits.
Click to reveal answer
intermediate
Why is it important to validate data on the server side in PHP?
Server-side validation ensures data is safe and correct even if client-side validation is bypassed. It protects against malicious input and errors.
Click to reveal answer
beginner
What is a common pattern to validate a required field in PHP?
Check if the field is set and not empty using isset() and !empty() functions before processing.
Click to reveal answer
Which PHP function validates an email format?
Ais_numeric()
Bctype_alpha()
Cfilter_var() with FILTER_VALIDATE_EMAIL
Dpreg_match() with /[0-9]/
✗ Incorrect
filter_var() with FILTER_VALIDATE_EMAIL checks if the input is a valid email address.
How do you check if a string contains only letters and numbers in PHP?
Afilter_var()
Bis_string()
Cstrlen()
Dctype_alnum()
✗ Incorrect
ctype_alnum() returns true if all characters are letters or digits.
What does the PHP function isset() check?
AIf a variable is set and not NULL
BIf a string is empty
CIf a number is positive
DIf an array contains values
✗ Incorrect
isset() returns true if the variable exists and is not NULL.
Why should validation be done on the server side?
ATo prevent bypassing client-side checks and protect data
BTo make the page load faster
CTo improve CSS styling
DTo reduce server load
✗ Incorrect
Server-side validation ensures security and data integrity even if client-side validation is skipped.
Which PHP function can be used to check if a string matches a pattern?
Astrlen()
Bpreg_match()
Cis_array()
Dtrim()
✗ Incorrect
preg_match() checks if a string matches a regular expression pattern.
Explain how to validate an email address in PHP and why it is important.
Think about built-in PHP functions for filtering.
You got /4 concepts.
Describe the difference between client-side and server-side validation and why server-side validation is necessary.
Consider what happens if a user disables JavaScript.
You got /4 concepts.
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
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.
Step 2: Identify the correct function usage
filter_var with FILTER_VALIDATE_EMAIL returns the email if valid, or false if invalid.
Final Answer:
filter_var with FILTER_VALIDATE_EMAIL -> Option B
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
Step 1: Identify the filter for URL validation
FILTER_VALIDATE_URL is the correct filter to check if a string is a valid URL.
Step 2: Confirm correct function call
filter_var takes the variable and the filter constant as parameters.
Final Answer:
filter_var($url, FILTER_VALIDATE_URL); -> Option C
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?
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
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.
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.