测试PHP中变量是否存在的最佳方法; isset()很明显坏了 [英] Best way to test for a variable's existence in PHP; isset() is clearly broken

查看:71
本文介绍了测试PHP中变量是否存在的最佳方法; isset()很明显坏了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

isset()文档:

isset() will return FALSE if testing a variable that has been set to NULL.

基本上,isset()根本不检查变量是否设置,而是检查是否将其设置为除NULL以外的任何值.

Basically, isset() doesn't check for whether the variable is set at all, but whether it's set to anything but NULL.

鉴于此,实际检查变量是否存在的最佳方法是什么?我尝试过类似的事情:

Given that, what's the best way to actually check for the existence of a variable? I tried something like:

if(isset($v) || @is_null($v))

(@是必需的,以避免在未设置$v时发出警告),但是is_null()isset()有类似的问题:未设置的变量将返回TRUE!似乎还:

(the @ is necessary to avoid the warning when $v is not set) but is_null() has a similar problem to isset(): it returns TRUE on unset variables! It also appears that:

@($v === NULL)

的工作方式与@is_null($v)完全相同,所以也可以了.

works exactly like @is_null($v), so that's out, too.

我们应该如何可靠地检查PHP中是否存在变量?

How are we supposed to reliably check for the existence of a variable in PHP?

未设置的变量与设置为NULL的变量之间在PHP中显然存在区别:

there is clearly a difference in PHP between variables that are not set, and variables that are set to NULL:

<?php
$a = array('b' => NULL);
var_dump($a);

PHP显示$a['b']存在,并且具有NULL值.如果您添加:

PHP shows that $a['b'] exists, and has a NULL value. If you add:

var_dump(isset($a['b']));
var_dump(isset($a['c']));

您可以看到我正在使用isset()函数讨论的模棱两可.这是这三个var_dump()s的输出:

you can see the ambiguity I'm talking about with the isset() function. Here's the output of all three of these var_dump()s:

array(1) {
  ["b"]=>
  NULL
}
bool(false)
bool(false)


进一步两件事.


Further edit: two things.

一个,用例.将数组转换为SQL UPDATE语句的数据,其中数组的键是表的列,数组的值是要应用于每一列的值.该表的任何列都可以保存NULL值,这是通过在数组中传递NULL值来表示的.您需要一种区分不存在的数组键和将数组的值设置为NULL的方法.这就是不更新列的值和将列的值更新为NULL之间的区别.

One, a use case. An array being turned into the data of an SQL UPDATE statement, where the array's keys are the table's columns, and the array's values are the values to be applied to each column. Any of the table's columns can hold a NULL value, signified by passing a NULL value in the array. You need a way to differentiate between an array key not existing, and an array's value being set to NULL; that's the difference between not updating the column's value and updating the column's value to NULL.

其次,

Second, Zoredache's answer, array_key_exists() works correctly, for my above use case and for any global variables:

<?php
$a = NULL;
var_dump(array_key_exists('a', $GLOBALS));
var_dump(array_key_exists('b', $GLOBALS));

输出:

bool(true)
bool(false)

由于可以正确处理几乎所有地方,因此我可以看到不存在的变量与设置为NULL的变量之间存在任何歧义,因此我将array_key_exists()称为PHP中最简单的官方方法真正检查变量是否存在.

Since that properly handles just about everywhere I can see there being any ambiguity between variables that don't exist and variables that are set to NULL, I'm calling array_key_exists() the official easiest way in PHP to truly check for the existence of a variable.

(我只能想到的其他情况是针对类属性的,其中有property_exists(),根据其文档array_key_exists()相似,因为它可以正确地区分未设置和设置为NULL.)

(Only other case I can think of is for class properties, for which there's property_exists(), which, according to its docs, works similarly to array_key_exists() in that it properly distinguishes between not being set and being set to NULL.)

推荐答案

如果您要检查的变量位于全局范围内,则可以执行以下操作:

If the variable you are checking would be in the global scope you could do:

array_key_exists('v', $GLOBALS) 

这篇关于测试PHP中变量是否存在的最佳方法; isset()很明显坏了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆