PHP find output programs (Filters) | set 1

Find the output of PHP programs | Filters | Set 1: Enhance the knowledge of PHP Filters by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 26, 2021

Question 1:

<?php
    $var = 100;
    
    $isValid = filter_var($var, FILTER_VALIDATE_INT);
    
    if ($isValid == false)
    {
        echo ("Integer is valid");
    }
    else
    {
        echo ("Integer is not valid");
    }
?>

Output:

Integer is not valid

Explanation:

In the above program, we created a variable $var that initialized with 100. Here, we used function filter_var() and pass filter constant FILTER_VALIDATE_INT, which is used to check given number is an integer or not.

In the above example, filter_var() function will return true and assigned to variable $isValid, but we checked false in the if condition that's why condition will false and else part will execute and print "Integer is not valid" on the webpage.

Question 2:

<?php
    $var = 0;
    
    $isValid = filter_var($var, FILTER_VALIDATE_INT);
    
    if ($isValid == true)
    {
        echo ("Integer is valid");
    }
    else
    {
        echo ("Integer is not valid");
    }
?>

Output:

Integer is not valid

Explanation:

The above program will print "Integer is not valid" on the webpage, because filter_var() function with FILTER_VALIDATE_INT returns false for number 0. Otherwise, it will return true for any integer number.

Question 3:

<?php
    $var = 3.14;
    
    $isValid = filter_var($var, FILTER_VALIDATE_DOUBLE);
    
    if ($isValid == true)
    {
        echo ("Given number is a valid floating point number");
    }
    else
    {
        echo ("Given number is not a valid floating point number");
    }
?>

Output:

Given number is not a valid floating-point number

Explanation:

In the above program, we used FILTER_VALIDATE_DOUBLE filter, but FILTER_VALIDATE_FLOAT filter is used to check floating-point numbers is valid or not, that's why function filter_var() will return false and "Given number is not a valid floating-point number" will print on the webpage.

Question 4:

<?php
    $email = "          [email protected]";
    $senitize_email = filter_var($email, FILTER_SANITIZE_EMAIL);
    
    echo "E-mail:" . $senitize_email;
?>

Output:

E-mail:[email protected]

Explanation:

The above program will print "E-mail:[email protected]" on the webpage, the FILTER_SANITIZE_EMAIL filter is used to sanitize the specified email address, it means all illegal characters will be removed from the e-mail address.

Question 5:

<?php
    $IP = "256.192.10.1";
    $URL = "http://www.includehelp.com";
    
    $isValid = filter_var($IP, FILTER_VALIDATE_IP);
    
    if ($isValid == true) echo "IP address is valid<br>";
    else echo "IP address is not valid<br>";
    
    $isValid = filter_var($IP, FILTER_VALIDATE_URL);
    if ($isValid == true) echo "URL is valid<br>";
    else echo "URL is not valid<br>";
?>

Output:

IP address is not valid
URL is not valid

Explanation:

In the above program, we create two variables $IP and $URL initialized with "256.192.10.1" and http://www.includehelp.com respectively. Here, we used filters FILTER_VALIDATE_IP and FILTER_VALIDATE_URL one by one. In the above program, we used an invalid IP address and a valid URL. That's why the below output will be printed on the webpage.

IP address is not valid
URL is not valid




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.