In PHP, to validate and sanitize external input filters are used. filter_var() function is used which perform both validate and sanitize external input. It takes two parameter:
- variable: It specify the variable you want to check.
- type: It specify the type to check
Syntax:
filter_var(variable, type)
Validate an Integer
For example:
<?php
$num = 500;
if (!filter_var($num, FILTER_VALIDATE_INT) === false) {
echo("Integer is valid");
} else {
echo("Integer is not valid");
}
?>
Sanitize and Validate an Email Address:
For example:
<?php
$email = "demo.test@demo.com";
// Remove illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate e-mail
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
?>
0 Comment(s)