问题描述
函数hello_testing(){
global $ conditional_random;
if(isset($ conditional_random)){
echofoo is inside;
$ b 全局变量(conditional_random)可能未被初始化调用 hello_testing()
函数。
那么,通过 isset()
何时 $ conditional_random
未初始化?它会失败还是永远是真的?
解决方案那么,为什么你不测试? ; - )
注意:不像您想象的那么容易 - 阅读完整答案; - )
调用 hello_testing();
函数,不设置变量:
hello_testing();
我没有输出 - 表示返回 false $
设置变量后调用该函数:
$ b $ b
$ conditional_random ='blah';
hello_testing();
我得到一个输出:
foo在
之内
这表示按预期工作,变量已设置 - 好吧,对此应该没有任何疑问^^
但请注意 isset
会返回 false
如果设置了变量,并且 null
!
请参阅
这意味着更好的测试是:
函数hello_testing(){
全球$ conditional_random;
var_dump($ conditional_random);
}
hello_testing();
并显示:
null
否注意:变量存在!即使 null
。
因为我没有在函数外设置变量,所以它显示 global
设置变量 - 但它并没有给它赋值;这意味着它是 null
,如果尚未设置在函数之外。
While:
函数hello_testing(){
// global $ conditional_random;
var_dump($ conditional_random);
}
hello_testing();
给出:
注意:未定义变量:conditional_random
证明已启用通知; - )
而且,如果全局变量不是 set ,则前面的示例会给出相同的通知。
最后:
函数hello_testing(){
global $ conditional_random;
var_dump($ conditional_random);
}
$ conditional_random ='glop';
hello_testing();
给出:
string'glop'(length = 4)
我的例子不被欺骗^^)
I have a question about global variable initialization.
function hello_testing() {
global $conditional_random;
if (isset($conditional_random)) {
echo "foo is inside";
}
}
The global variable (conditional_random) may not be initialized before the hello_testing()
function is called.
So, what happens to my validation via isset()
when $conditional_random
is not initialized? Will it fail or it will always be true?
解决方案 Well, why don't you just test ? ;-)
Note : Not as easy as you'd think -- read the full answer ;-)
Calling the hello_testing();
function, without setting the variable :
hello_testing();
I get no output -- which indicates isset
returned false
.
Calling the function, after setting the variable :
$conditional_random = 'blah';
hello_testing();
I get an output :
foo is inside
Which indicates global
works as expected, when the variable is set -- well, one should not have any doubt about that ^^
BUT note that isset
will return false
if a variable is set, and null
!
See the manual page of isset()
Which means that a better test would be :
function hello_testing() {
global $conditional_random;
var_dump($conditional_random);
}
hello_testing();
And this displays :
null
No Notice : the variable exists ! Even if null
.
As I didn't set the variable outside of the function, it shows that global
sets the variable -- but it doesn't put a value into it ; which means it's null
if not already set outside the function.
While :
function hello_testing() {
//global $conditional_random;
var_dump($conditional_random);
}
hello_testing();
Gives :
Notice: Undefined variable: conditional_random
Proves that notices are enabled ;-)
And, if global didn't "set" the variable, the previous example would have given the same notice.
And, finally :
function hello_testing() {
global $conditional_random;
var_dump($conditional_random);
}
$conditional_random = 'glop';
hello_testing();
Gives :
string 'glop' (length=4)
(Purely to demonstrate my example is not tricked ^^ )
这篇关于isset()和PHP全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!