好吧,你为什么不测试一下呢? ;-)
Note:这并不像你想象的那么容易——阅读完整的答案;-)
Calling the `hello_testing();` function, without setting the variable:
hello_testing();
我没有得到任何输出——这表明isset https://www.php.net/isset回false
.
Calling the function, after setting the variable:
$conditional_random = 'blah';
hello_testing();
我得到一个输出:
foo is inside
这表明global http://fr.php.net/manual/en/language.variables.scope.php设置变量后,按预期工作——嗯,这一点应该没有任何疑问^^
但请注意isset
将返回false
如果设置了变量,并且null
!
See the 的手册页isset() https://www.php.net/isset
这意味着更好的测试是:
function hello_testing() {
global $conditional_random;
var_dump($conditional_random);
}
hello_testing();
这显示:
null
无通知:变量存在!即使null
.
由于我没有在函数外部设置变量,所以它表明global
sets变量——但它没有给它赋值;这意味着它是null
如果尚未在函数外部设置。
While:
function hello_testing() {
//global $conditional_random;
var_dump($conditional_random);
}
hello_testing();
Gives:
Notice: Undefined variable: conditional_random
这证明通知已启用;-)
而且,如果全球没有“set" 变量,前面的示例会给出相同的通知。
And, finally:
function hello_testing() {
global $conditional_random;
var_dump($conditional_random);
}
$conditional_random = 'glop';
hello_testing();
Gives:
string 'glop' (length=4)
(这纯粹是为了证明我的例子没有被欺骗^^)