Open In App

Output of PHP programs | Set 2 ( Filters )

Last Updated : 07 Mar, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Predict the output of the following PHP programs:
Question 1




<?php
    $num = "123";
    if (!filter_var($num, FILTER_VALIDATE_INT))
        echo("Hello");
    else
        echo("Welcome to GeeksforGeeks");
?>


Options:

  1. No output is returned
  2. Hello
  3. Welcome to GeeksforGeeks
  4. Error

Output:

Welcome to GeeksforGeeks

Explanation: filter_var() – Filters a single variable with a specified filter.

Question 2




<?php
    $var=300;
    $int_options = array("options"=>array ("min_range"=>0, "max_range"=>256));
    if (!filter_var($var, FILTER_VALIDATE_INT, $int_options))
        echo("Hello");
    else
        echo("Welcome to GeeksforGeeks");
?>


Options:

  1. No output is returned
  2. Hello
  3. Welcome to GeeksforGeeks
  4. Error

Output:

Hello

Explanation: Since the integer is “300” it is not in the specified range, and the output of the code above will be: “Integer is not valid”.

Question 3




<?php
    $string = "Welcomeêê to GeêêeksfoøørGeêêeks";
    $string = filter_var($string, FILTER_SANITIZE_EMAIL);
    echo $string;
?>


Options:

  1. Welcomeêê to GeêêeksfoøørGeêêeks
  2. Welcomeee to GeeeeksfooorGeeeeks
  3. Welcomeê to GeêeksfoørGeêeks
  4. WelcometoGeeksforGeeks

Output:

WelcometoGeeksforGeeks

Explanation: Sanitize is nothing but take away invalid characters or special characters so therefore the invalid characters like ê and ø and space will be removed.

Question 4




<?php
    $value = 'GeeksforGeeks';
    $result = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
?>


Options:

  1. FALSE
  2. TRUE
  3. No Output
  4. ERROR

Output:

No Output

Explanation: There is an undocumented filter flag for FILTER_VALIDATE_BOOLEAN. The documentation implies that it will return NULL if the value doesn’t match the allowed true/false values. However this doesn’t happen unless you give it the FILTER_NULL_ON_FAILURE flag. Hence the output will be No Output.

Question 5




<?php
    function GeeksforGeeks($string)
    {
        return str_replace("_", " ", $string);
    }
    $string = "I_am_intern_at_GeeksforGeeks!";
    echo filter_var($string, FILTER_CALLBACK, array("options"=>"GeeksforGeeks"));
?>


Options:

  1. I_am_intern_at_GeeksforGeeks!
  2. IaminternatGeeksforGeeks!
  3. I am intern at GeeksforGeeks!
  4. Error

Output:

I am intern at GeeksforGeeks!

Explanation: The code above converts all “_” to white spaces. Call the filter_var() function with the FILTER_CALLBACK filter and an array containing our function.

Question 6




<?php
    $num = '123+abc-xyz*';
    $num = filter_var($num, FILTER_SANITIZE_NUMBER_INT);
    echo $num
?>


Options:

  1. 123+abc-xyz*
  2. abcxyz*
  3. 123+-
  4. Error

Output:

123+-

Explanation: filter_var() – with FILTER_SANITIZE_NUMBER_INT, Remove all characters except digits, +- and optionally.

Question 7




<?php
    $num = '123+-abc*';
    $res = filter_var($num, FILTER_SANITIZE_NUMBER_FLOAT);
    echo $res
?>


Options:

  1. 123+abc-xyz*
  2. abcxyz*
  3. 123+-
  4. Error

Output:

123+-

Explanation: filter_var() – with FILTER_SANITIZE_NUMBER_FLOAT, Remove all characters except digits, +- and optionally.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads