Output of PHP programs | Set 2 ( Filters )

Predict the output of the following PHP programs:
Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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


filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.



My Personal Notes arrow_drop_up

Intern at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.