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

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

问题描述

来自 isset() 文档:

From the isset() docs:

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?

PHP 中未设置的变量和设置为 NULL 的变量之间显然存在差异:

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.

第二,Zoredache 的答案array_key_exists() 工作正常,适用于我的上述用例和任何全局变量:

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天全站免登陆