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:
- No output is returned
- Hello
- Welcome to GeeksforGeeks
- 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:
- No output is returned
- Hello
- Welcome to GeeksforGeeks
- 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:
- Welcomeêê to GeêêeksfoøørGeêêeks
- Welcomeee to GeeeeksfooorGeeeeks
- Welcomeê to GeêeksfoørGeêeks
- 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:
- FALSE
- TRUE
- No Output
- 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:
- I_am_intern_at_GeeksforGeeks!
- IaminternatGeeksforGeeks!
- I am intern at GeeksforGeeks!
- 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:
- 123+abc-xyz*
- abcxyz*
- 123+-
- 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:
- 123+abc-xyz*
- abcxyz*
- 123+-
- Error
Output:
123+-
Explanation: filter_var() – with FILTER_SANITIZE_NUMBER_FLOAT, Remove all characters except digits, +- and optionally.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Mar, 2018
Like Article
Save Article