PHP Filters Advanced
- What will be the output of the following PHP code?
<?php
function convertSpace($str)
{
return str_replace("_", " ", $str);
}
$str = "Interview_Mania_is_good_for_interview_preparation!";
echo filter_var($str, FILTER_CALLBACK, array("options"=>"convertSpace"));
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
The code above converts all “_” to white spaces. Call the filter_var() function with the FILTER_CALLBACK filter and an array containing our function.
- What will be the output of the following PHP code?
<?php
$var = 'vehicle';
$Res = filter_var($var, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
echo $Res;
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
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.
- What will be the output of the following PHP code?
<?php
$n=256;
$int_opt = array("opt"=>array ("min"=>0, "max"=>256));
if (!filter_var($n, FILTER_VALIDATE_INT, $int_opt))
echo("Given Integer is not valid");
else
echo("Given Integer is valid");
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
Since the integer is “256” it is in the specified range, and the output of the code above will be: “Given Integer is valid”.
- What will be the output of the following PHP code?
<?php
$n = "102030";
if (!filter_var($n, FILTER_VALIDATE_INT))
echo("Given Integer is not valid");
else
echo("Above Integer is valid");
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
filter_var() – Filters a single variable with a specified filter.