Q:

PHP find output programs (Filters) | set 1

belongs to collection: PHP Find Output Programs

0

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.

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");
    }
?>

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");
    }
?>

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");
    }
?>

Question 4:

<?php
    $email = "          ABC@gmail.com";
    $senitize_email = filter_var($email, FILTER_SANITIZE_EMAIL);
    
    echo "E-mail:" . $senitize_email;
?>

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>";
?>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

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.

Answer 2:

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.

Answer 3:

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.

Answer 4:

Output:

E-mail:ABC@gmail.com

Explanation:

The above program will print "E-mail:ABC@gmail.com" 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.

Answer 5:

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Find Output Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
PHP find output programs (Filters) | set 2... >>
<< PHP find output programs (Exceptions) | set 2...