Validation

Example #1 Validating email addresses with filter_var()

<?php
$email_a 
'joe@example.com';
$email_b 'bogus';

if (
filter_var($email_aFILTER_VALIDATE_EMAIL)) {
    echo 
"Email address '$email_a' is considered valid.\n";
}
if (
filter_var($email_bFILTER_VALIDATE_EMAIL)) {
    echo 
"Email address '$email_b' is considered valid.\n";
} else {
    echo 
"Email address '$email_b' is considered invalid.\n";
}
?>

The above example will output:

Email address 'joe@example.com' is considered valid.
Email address 'bogus' is considered invalid.

Example #2 Validating IP addresses with filter_var()

<?php
$ip_a 
'127.0.0.1';
$ip_b '42.42';

if (
filter_var($ip_aFILTER_VALIDATE_IP)) {
    echo 
"IP address '$ip_a' is considered valid.";
}
if (
filter_var($ip_bFILTER_VALIDATE_IP)) {
    echo 
"IP address '$ip_b' is considered valid.";
}
?>

The above example will output:

IP address '127.0.0.1' is considered valid.

Example #3 Passing options to filter_var()

<?php
$int_a 
'1';
$int_b '-1';
$int_c '4';
$options = array(
    
'options' => array(
        
'min_range' => 0,
        
'max_range' => 3,
    )
);
if (
filter_var($int_aFILTER_VALIDATE_INT$options) !== FALSE) {
    echo 
"Integer A '$int_a' is considered valid (between 0 and 3).\n";
}
if (
filter_var($int_bFILTER_VALIDATE_INT$options) !== FALSE) {
    echo 
"Integer B '$int_b' is considered valid (between 0 and 3).\n";
}
if (
filter_var($int_cFILTER_VALIDATE_INT$options) !== FALSE) {
    echo 
"Integer C '$int_c' is considered valid (between 0 and 3).\n";
}

$options['options']['default'] = 1;
if ((
$int_c filter_var($int_cFILTER_VALIDATE_INT$options)) !== FALSE) {
    echo 
"Integer C '$int_c' is considered valid (between 0 and 3).";
}
?>

The above example will output:

Integer A '1' is considered valid (between 0 and 3).
Integer C '1' is considered valid (between 0 and 3).

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial