Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • filter_var() Function

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 89
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: