检查变量是否在 PHP 中 is_undefined

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16993027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:01:49  来源:igfitidea点击:

Check if variable is_undefined in PHP

phpissetisnullisnullorempty

提问by tjbourke

In PHP, I want to check if a variable has not been set/defined, where setting a variable NULL is considered set/defined.

在 PHP 中,我想检查是否尚未设置/定义变量其中设置变量 NULL 被视为 set/defined

I'm aware everything here: http://php.net/manual/en/types.comparisons.phpincluding isset(), empty(), and is_null(). None of these appear to be what I'm looking for. Consider the following example:

我知道这里的一切:http: //php.net/manual/en/types.comparisons.php包括isset()、empty() 和is_null()。这些似乎都不是我正在寻找的。考虑以下示例:

<?php 
$myNull = null;
echo 'isset($myNull): "'.isset($myNull).'"<br />';
echo '$myNull value = "'.$myNull . '"<br />';

echo "<br />";

echo 'isset($myUndefined): "'.isset($myUndefined).'"<br />';
echo '$myUndefined value = "'.$myUndefined . '"<br />';
?>

This example outputs something like:
isset($myNull): ""
$myNull value = ""

此示例输出如下内容:
isset($myNull): ""
$myNull value = ""

isset($myUndefined): ""
Notice: Undefinedvariable: myUndefined in C:\wamp\www\plm\temp4.php on line 9
$myUndefined value = ""

isset($myUndefined): ""
注意:未定义变量:myUndefined in C:\wamp\www\plm\temp4.php on line 9
$myUndefined value = ""

I want to know if a variable is Undefinedas it says above in the notice. I want a function, call it "is_undefined", where

我想知道一个变量是否如上面通知中所说的那样未定义。我想要一个函数,称之为“is_undefined”,其中

$myNull = null;
is_undefined($myNull); // is false
is_undefined($myUndefined); // is true

Anyone? Thanks in advance.

任何人?提前致谢。

回答by cljk

I haven′t used it yet - but I think that "get_defined_vars" should be worth a look... http://php.net/manual/en/function.get-defined-vars.php

我还没有使用它 - 但我认为“get_defined_vars”应该值得一看...... http://php.net/manual/en/function.get-defined-vars.php

I would give it a try and dump the result.

我会试一试并转储结果。

回答by Alain Tiemblo

I think that get_defined_varsis a good candidate for such job:

我认为get_defined_vars是此类工作的理想人选:

array_key_exists('myNull', get_defined_vars());

Should do what you expect.

应该做你期望的。

If you work on a global context, you can also use:

如果您在全局上下文中工作,您还可以使用:

array_key_exists('myNull', $GLOBALS);

回答by chrislondon

If you want an is_undefined function I would prefer not to work with arrays so I would do this:

如果你想要一个 is_undefined 函数,我宁愿不使用数组,所以我会这样做:

function is_undefined(&$test) {
    return isset($test) && !is_null($test);
}

So when you echo isset($myNull);it converts the boolean(true) to "". thats why the value is blank. If you want to see it on the screen you can do var_dump(isset($myNull));that will display if it's true or false.

因此,当您echo isset($myNull);将布尔值(真)转换为“”时。这就是为什么该值为空。如果你想在屏幕上看到它,你可以这样做var_dump(isset($myNull));,它会显示它是真还是假。

Also you have an echo of $myUndefined but it's not set yet so that's why you get a warning. What you want to do is:

此外,您还有 $myUndefined 的回声,但尚未设置,因此您会收到警告。你想要做的是:

if (!empty($myUndefined)) {
    // variable is defined so do something with it
    echo '$myUndefined value = "' . $myUndefined . '"<br />';
} else {
    echo 'Oops, $myUndefined is Undefined!<br />";
}

Here is a brief overview of isset() vs. is_null() vs. empty()

以下是 isset() 与 is_null() 与 empty() 的简要概述

$foo = null;
// isset($foo) == true;
// empty($foo) == true;
// is_null($foo) == true;

// Notice I don't set $foo2 to anything
// isset($foo2) == false;
// empty($foo2) == true;
// is_null($foo2) throws a notice!

$foo3 = false;
// isset($foo2) == true;
// empty($foo2) == true;
// is_null($foo2) == false;

$foo4 = 1234;
// isset($foo2) == true;
// empty($foo2) == false;
// is_null($foo2) == false;

回答by Crisp

You can use compact()for this too, if the variable you give it isn't in the symbol table it returns an empty array, otherwise an array containing the variable name/value pair, just cast the result to a boolean

你也可以使用compact()这个,如果你给它的变量不在符号表中,它返回一个空数组,否则一个包含变量名称/值对的数组,只需将结果转换为布尔值

<?php

$myNull = null;

$isDefined = (bool) compact('myNull'); // true

$otherIsDefined = (bool) compact('myUndefined'); // false 

回答by John Yin

Yes, just like Mr. Jonathan mentioned above, we could use array_key_exists() + $GLOBALS rather than get_defined_vars() to identify Undefined variable to null

是的,就像上面 Jonathan 先生提到的,我们可以使用 array_key_exists() + $GLOBALS 而不是 get_defined_vars() 来将 Undefined 变量标识为 null

$x;  // $x is undefined 
$y=null;  // $y is defined and is NULL type variable with the only null value
$z=[];    // $z is an array object

if( array_key_exists('x', $GLOBALS) && is_null($x) ) echo "$x exists and is null\n";
if( array_key_exists('y', $GLOBALS) && is_null($y) ) echo "$y exists and is null\n";
if( array_key_exists('z', $GLOBALS) && is_null($z) ) echo "$he exists and is null\n";

// output 
$y exists and is null

回答by GGio

If you are using OOP then use overloading __isset()this function will execute when you are trying to access a variable that is not defined anywhere. example:

如果您使用的是 OOP,那么__isset()当您尝试访问未在任何地方定义的变量时,将使用重载此函数。例子:

 public function __isset($name) {
    echo 'Hello';
 }

Thus, will avoid any error message or notice related to undefined variable

因此,将避免与未定义变量相关的任何错误消息或通知