PHPのお勉強!

PHP TOP

filter_has_var

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

filter_has_var指定した型の変数が存在するかどうかを調べる

説明

filter_has_var(int $input_type, string $var_name): bool

パラメータ

input_type

INPUT_GETINPUT_POSTINPUT_COOKIEINPUT_SERVERINPUT_ENV のいずれか。

var_name

調べたい変数の名前。

戻り値

成功した場合に true を、失敗した場合に false を返します。

add a note

User Contributed Notes 2 notes

up
33
drm at melp dot nl
15 years ago
Please note that the function does not check the live array, it actually checks the content received by php:

<?php
$_GET
['test'] = 1;
echo
filter_has_var(INPUT_GET, 'test') ? 'Yes' : 'No';
?>

would say "No", unless the parameter was actually in the querystring.

Also, if the input var is empty, it will say Yes.
up
19
nanhe dot kumar at gmail dot com
11 years ago
Through this example i think you can better understand

if ( !filter_has_var(INPUT_GET, 'email') ) {
echo "Email Not Found";
}else{
echo "Email Found";
}
Output

localhost/nanhe/test.php?email=1 //Email Found
localhost/nanhe/test.php?email //Email Found
http://localhost/nanhe/test.php //Email Not Found

Consider on second example

http://localhost/nanhe/test.php
$_GET['email']="info@nanhe.in";
if ( !filter_has_var(INPUT_GET, 'email') ) {
echo "Email Not Found";
}else{
echo "Email Found";
}
But output will be Email Not Found
To Top