PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
102: filter_var for Input Validation
Why should I use filter_var instead of just writing a regex?
I've seen plenty of developers spend hours crafting the "perfect" regular expression to validate an email address, only to find out that some perfectly valid email format breaks their code. Honestly, it's a rabbit hole you don't want to go down. PHP's filter_var() is essentially a wrapper around a set of proven, standardized validation rules. It's faster to write, easier for the next person on your team to read, and significantly less prone to the edge-case bugs that plague custom regex.
// The "hard" way (regex)
if (preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/", $email)) {
/* ... */
}
// The professional way
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "That looks like a real email.";
}
What is the actual difference between validating and sanitizing?
This is a common point of confusion. Think of validation as a gatekeeper: it asks "Is this input acceptable?" and returns a boolean (or the value if it's valid). If the input is bad, validation tells you to reject it entirely.
Sanitization, on the other hand, is like a filter. It doesn't tell you if the data is "right" or "wrong"; it just strips out the characters that shouldn't be there. I usually sanitize first to clean up accidental whitespace or illegal characters, and then validate to ensure the result is actually useful.
$input = " user@example.com "; // Note the leading/trailing spaces
// Sanitization: removes illegal characters from email
$clean_email = filter_var($input, FILTER_SANITIZE_EMAIL);
// Validation: checks if the cleaned string is actually a valid email format
if (filter_var($clean_email, FILTER_VALIDATE_EMAIL)) {
echo "Clean and valid!";
}
Can I use this for things other than emails and URLs?
Absolutely. It's incredibly useful for numbers. For instance, if you're building a form where a user enters their age, you don't just want to know if it's an integer; you want to know if it's a reasonable integer. You can pass an options array to filter_var to define a specific range.
I use this all the time for pagination or age-restricted content. It saves me from writing a bunch of if ($age > 0 && $age < 120) statements.
$age = "25";
$options = [
'options' => [
'min_range' => 1,
'max_range' => 120
]
];
if (filter_var($age, FILTER_VALIDATE_INT, $options)) {
echo "Age is a valid integer within the human lifespan.";
} else {
echo "Please enter a realistic age.";
}📋 Practical Task
Exercise: Build a User Profile Submission Validator
Imagine you are building a registration page. Create a PHP script that processes a mock $_POST array containing the following fields: email, website, and experience_years.
- Email: Sanitize the input first, then validate it.
- Website: Validate that it is a properly formatted URL.
- Experience Years: Validate that it is an integer between 0 and 50.
Your script should collect any validation errors into an array. If the array is empty, print "Profile is valid!"; otherwise, print a list of all the specific errors found.
There are no comments for now.