Output of PHP programs | Set 2 ( Filters )
Last Updated :
07 Mar, 2018
Predict the output of the following PHP programs:
Question 1
PHP
<?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
<?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
<?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
<?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
<?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
<?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
<?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.
Similar Reads
Output of PHP programs | Set 3 Predict the output of below PHP programs: Question 1 PHP <?php $number = array(0, 1, one, two, three, 5); $num = preg_grep("/[0-5]/", $number); print_r($num); ?> Options: Array([0]=>0 [1]=>1 [2]=>one [3]=>two [4]=>three [5]=>5) Array([2]=>one [3]=>two [4]=>
3 min read
Output of PHP programs | Set 1 (Regular Expressions) Predict the output of following PHP programs: Question 1 PHP <?php echo str_pad("Welcome", 5)." to GeeksforGeeks."; ?> Options: WelcomeWelcomeWelcomeWelcomeWelcome to GeeksforGeeks. to GeeksforGeeks. WelcomeWelcomeWelcomeWelcomeWelcome to GeeksforGeeks. Welcome Welcome to G
3 min read
PHP | stream_get_filters() Function The stream_get_filters() function is an inbuilt function in PHP which is used to get the list of registered stream filters. Syntax: array stream_get_filters( void ) Parameters: This function does not accept any parameter. Return Value: This function returns an array containing the name of all availa
1 min read
PHP | preg_filter() Function The preg_filter() function is an inbuilt function in PHP which is used to perform a regular expression search and replace the text. Syntax: preg_filter( $pattern, $replacement, $subject, $limit, $count ) Parameters: This function accepts five parameters as mention above and describe below. $pattern:
2 min read
PHP | filter_input() Function The filter_input() is an inbuilt function in PHP which is used to get the specific external variable by name and filter it. This function is used to validate variables from insecure sources, such as user input from form. This function is very much useful to prevent some potential security threat lik
2 min read