Validation helps check if data is correct before using it. It stops mistakes and keeps programs safe.
Common validation patterns in PHP
<?php // Example of validating an email if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // valid email } else { // invalid email } ?>
filter_var function for common validations like email and URL.preg_match for custom patterns.<?php // Validate email $email = 'user@example.com'; if (filter_var($email, FILTER_VALIDATE_EMAIL)) { echo 'Valid email'; } else { echo 'Invalid email'; }
<?php // Validate number range $age = 25; if ($age >= 18 && $age <= 99) { echo 'Age is valid'; } else { echo 'Age is invalid'; }
<?php // Validate non-empty string $name = 'John'; if (!empty($name)) { echo 'Name is provided'; } else { echo 'Name is missing'; }
<?php // Validate pattern with regex $phone = '123-456-7890'; if (preg_match('/^\d{3}-\d{3}-\d{4}$/', $phone)) { echo 'Phone number is valid'; } else { echo 'Phone number is invalid'; }
This program checks if the email is correct, password is at least 8 characters, and age is between 18 and 99.
<?php // Sample program to validate user input $email = 'test@example.com'; $password = 'Pass1234'; $age = 20; // Validate email if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo "Email is invalid\n"; } else { echo "Email is valid\n"; } // Validate password length if (strlen($password) < 8) { echo "Password too short\n"; } else { echo "Password length is okay\n"; } // Validate age range if ($age < 18 || $age > 99) { echo "Age is invalid\n"; } else { echo "Age is valid\n"; }
Always sanitize data before validating to avoid security issues.
Use built-in PHP filters when possible for simplicity and reliability.
Regular expressions are powerful but can be tricky; test them carefully.
Validation checks if data is correct before using it.
Use filter_var for common checks like email and URL.
Use preg_match for custom pattern checks.
Practice
Which PHP function is commonly used to validate if a string is a valid email address?
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 BQuick Check:
Email validation = filter_var with FILTER_VALIDATE_EMAIL [OK]
- Using preg_match with wrong regex for email
- Checking length instead of format
- Using string functions unrelated to validation
Which of the following is the correct syntax to validate a URL using filter_var in PHP?
?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 CQuick Check:
URL validation = filter_var with FILTER_VALIDATE_URL [OK]
- Using FILTER_VALIDATE_EMAIL for URLs
- Using integer or boolean filters incorrectly
- Missing the filter parameter
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';
}Solution
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.Step 2: Check the input string against the pattern
The string '123-456-7890' matches this pattern exactly.Final Answer:
Valid phone -> Option DQuick Check:
Regex matches phone format = Valid phone [OK]
- Forgetting anchors ^ and $ in regex
- Using wrong quantifiers in regex
- Not escaping special characters
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';
}Solution
Step 1: Understand filter_var with FILTER_VALIDATE_INT
This filter returns the integer value if valid, or false if invalid.Step 2: Check input '123a'
The string '123a' is not a valid integer, so filter_var returns false.Final Answer:
filter_var returns false for '123a', so output is 'Invalid integer' -> Option AQuick Check:
Invalid integer string returns false = 'Invalid integer' output [OK]
- Using FILTER_VALIDATE_NUMBER which does not exist
- Casting before validation causing wrong results
- Assuming filter_var always returns boolean
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?
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.Final Answer:
/^[a-zA-Z][a-zA-Z0-9_]{4,14}$/ -> Option AQuick Check:
Start letter + allowed chars + length 5-15 = /^[a-zA-Z][a-zA-Z0-9_]{4,14}$/ [OK]
- Allowing digits as first character
- Not enforcing length limits
- Missing anchors ^ and $
Ready to prove it?
Pass the test and this pattern moves unknown
