php
$num
=
"123"
;
if
(! filter_var (
$num
, FILTER_VALIDATE_INT))
echo
(
"Hello"
);
else
echo
(
" Welcome to GeeksforGeeks "
);
?>
Parameters:
No output is returned Hello Welcome to GeeksforGeeks error Output:
Welcome to GeeksforGeeks
Explanation:filter_var() - Filters one variable with the 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 "
);
?>
Parameters:
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 above code will look like "The integer is not valid."
Question 3 php
$string
=
"Welcomeêê to GeêêeksfoøørGeêêeks"
;
$string
= filter_var (
$string
, FILTER_SANITIZE_EMAIL);
echo
$string
;
?>
Parameters:
Welcome to GeêêeksfoøørGeêêeks Welcome to GeeeeksfooorGeeeeks Welcome to GeêeksfoørGeêeks WelcometoGeeksforGeeks Output:
WelcometoGeeksforGeeks
Explanation:Sanitize - this is nothing more than removing invalid characters or special characters, so invalid characters such as ê and ø and space will be removed.
Question 4 php
$value
=
’GeeksforGeeks’
;
$result
= filter_var (
$value
, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
?>
Parameters:
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 is not true / false. However, this will not happen unless you give it the FILTER_NULL_ON_FAILURE flag. Therefore, the result 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"
));
?>
Parameters: I_am_intern_at_GeeksforGeeks! IaminternatGeeksforGeeks! I’m an intern at GeeksforGeeks! error Output: I am intern at GeeksforGeeks!
Explanation:The above code converts all "_" characters to 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
?>
Parameters: 123 + ABC-XYZ * abcxyz * 123 + - error Exit: 123 + -
Explanation:filter_var() - with FILTER_SANITIZE_NUMBER_INT, remove all characters except numbers, + - and if necessary.Question 7 Php
$num
=
’123 + -abc *’
;
$res
= filter_var (
$num
, FILTER_SANITIZE_NUMBER_FLOAT);
echo
$res
?>
Parameters: 123 + ABC-XYZ * abcxyz * 123 + - error Exit: 123 + -
Explanation:filter_var() - with FILTER_SANITIZE_NUMBER_FLOAT, remove all characters except numbers, + - and if necessary.