本文实例讲述了php过滤器 filter_has_var() 函数用法。分享给大家供大家参考,具体如下:

定义和用法

filter_has_var() 函数检查是否存在指定输入类型的变量。

如果成功则返回 true,如果失败则返回 false。

语法

filter_has_var(type, variable)

  • 第一个参数type(必须):规定要检查的类型,可以检查的类型有input_get、input_post、input_cookie、input_server、input_env
  • 第二个参数variable(必须):需要检查的变量

例子:

<?php
  if(!filter_has_var(input_get, "name")) {
    echo("input type does not exist");
  }
  else {
    echo("input type exists");
  }

地址栏输入链接:

localhost://test.php?name=test

输出结果:

input type exists

使用此函数可以用来检查是否是get或post提交以及是否有cookie变量存在。

当然,你也可以使用 isset($_get[“name”]) 进行判断

  // please note that the function does not check the live array, 
  // it actually checks the content received by php:
  $_get['name'] = 1;
  echo filter_input(input_get, 'name') ? 'yes' : 'no';

输出结果:

no