略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: filter_has_var

2024-05-02

filter_has_var

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

filter_has_var检测是否存在指定类型的变量

说明

filter_has_var(int $type, string $variable_name): bool

参数

type

INPUT_GETINPUT_POSTINPUT_COOKIEINPUT_SERVERINPUT_ENV 里的其中一个。

variable_name

要检查的变量名。

返回值

成功时返回 true, 或者在失败时返回 false

add a noteadd a note

User Contributed Notes 3 notes

up
30
drm at melp dot nl
13 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
18
nanhe dot kumar at gmail dot com
9 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
up
-11
dezlov
6 years ago
For people who are wondering whether "magic_quotes_gpc" has any affect on "filter_input" operation, here is an extract from a PHP source file:

* As the input filter acts on input data before the magic quotes function mangles data, all access through the filter() function will not have any quotes or slashes added - it will be the pure data as send by the browser.

So when using this function, there is no need to strip the slashes when "magic_quotes_gpc" is enabled.

官方地址:https://www.php.net/manual/en/function.filter-has-var.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3