PHP CHAPTER 27

PHP Form URL/E-mail

PHP provides built-in filters for validating email addresses and URLs.

📘

What is it?

PHP provides built-in filters for validating email addresses and URLs.

💡

Why do we use it?

Built-in filters provide a convenient way to validate common data formats.

⚙️

How does it work?

filter_var checks the supplied value against a selected validation filter.

🧩

Syntax

PHP Syntax
filter_var($email, FILTER_VALIDATE_EMAIL);
filter_var($url, FILTER_VALIDATE_URL);
💻

Example

PHP Example
<?php

$email = "student@example.com";
$url = "https://example.com";

echo filter_var($email, FILTER_VALIDATE_EMAIL) ? "Valid Email" : "Invalid Email";
echo "<br>";
echo filter_var($url, FILTER_VALIDATE_URL) ? "Valid URL" : "Invalid URL";

?>
🔍

Example Explained

The first filter validates email format. The second validates URL format.
🚀

More Examples

<?php $email = "wrong-email"; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) echo "Invalid"; ?>

⚠️ Important Rules

Validation checks format; it does not prove that an email address actually exists.

❌ Common Mistakes

Assuming a syntactically valid email is automatically a real mailbox.

🌍 Real-World Use

Profile pages, registration and website fields.

📝 Practice Exercise

Build a form containing email and website fields.

💻 Try It Yourself

Write and run this PHP lesson inside the AIFA Coding Lab.

Open Coding Lab →