设置 $var 是什么意思?

What does it mean that $var is set?

设置或未设置 $var 的术语是什么意思(在 PHP 或编程中的一般术语)?

我在这里http://www.php.net/manual/en/types.comparisons.php找到了一些比较table,但有时它让我感到困惑。

这是否意味着变量在声明时设置并赋值,或者在未声明或声明但未赋值时不设置?

isset() 是 PHP 中的一个函数,如果变量(在本例中为 $var)被赋值,它将 return 为真。如果变量已创建但未分配任何值,值为 null 或未定义,它将 return false。基本上,isset($var) 表示这个变量是否可以安全使用。

更新

为了解释 NULL 值和 undefined 之间的差异,Jonathan Kuhn 在上面的提交中提供了以下代码。

<?php
//test 1 is defined, but has a value of null. isset will return false, use causes no error.
$test1 = null;
var_dump($test1);
var_dump(isset($test1));

echo "\n----------\n\n";

//test2 is defined with a string value. isset will return true
$test2 = "test";
var_dump($test2);
var_dump(isset($test2));

echo "\n----------\n\n";

//test3 is not defined, isset returns false and use causes error.
var_dump($test3);
var_dump(isset($test3));

将输出:

NULL
bool(false)

----------

string(4) "test"
bool(true)

----------


Notice: Undefined variable: test3 in /in/Nmk0t on line 17
NULL
bool(false)

基本上是未声明、未赋值或未设置NULL的变量。

为了证明比较 table 你可以用 isset()

来测试
if (isset($var)) {
    echo "it is set.";
}

这个答案只是为了更清楚地说明php的truty/falseisset()函数,@thatguy发表了一个很好的答案,这只是提供了例子。

These are a few examples about how php works with true/false scenarios

检查 link 代码以获得此输出。

您从具有以下变量的循环中获得此输出:

$var1 = 0;
$var2 = 1;
$var3 = null;
$var4;
$var5 = false;
$var6 = true;



Examples about isset(), and truthy/falsy comparitions
-----------------------------------------------------
About isset() function
----------------------
var1 is declared and has a value, value = 0
var2 is declared and has a value, value = 1
var3 is declared with no value or is null
var4 is declared with no value or is null
var5 is declared and has a value, value = 
var6 is declared and has a value, value = 1
----------------------
About true/false cases
----------------------
var1 is declared with no value, is null or equal to zero or false
var2 is declared and has a value, value = 1
var3 is declared with no value, is null or equal to zero or false
var4 is declared with no value, is null or equal to zero or false
var5 is declared with no value, is null or equal to zero or false
var6 is declared and has a value, value = 1
About asking to a never ever writen variable
--------------------------------------------
no idea where this variable is
no idea where this variable is

这取决于你"set"的意思。

如果你的意思是"I can use the variable in my code without generating undefined variable notices",那么当变量有任何值时,它就会被设置。你不能在 PHP 中声明一个变量而不给它赋值。例如:

<?php

$var;

不创建 $var 变量。尝试将该 $var 用于该行代码之后的任何内容都会给您一个未定义的变量通知,如果您 print_r(get_defined_vars()); 您将得到一个空数组。

如果您的意思是“isset($var) 将 return true”,那么当变量具有 null 以外的任何值时,它就会被设置。例如:

<?php

$varX = null;
$varY = 0;

有了这些变量,两者都被定义了。您可以在代码中使用它们而不会收到未定义变量通知。 print_r(get_defined_vars()); 会告诉你 Array ( [varX] => [varY] => 0 )。但是 isset($varX) returns false 即使定义了 $varX,因为它有一个 null 值。

一旦您为变量赋值,它将在其范围内保持定义,除非您使用unset($var).

显式取消设置

唯一可以声明变量而不显式赋值的情况是它是 class 属性。例如:

class Example
{
    public $var;

}

$ex = new Example;
var_dump($ex->var);

此处$var在声明时隐式赋值为null,在你的代码中引用它不会引起未定义变量通知。