php变量范围 [英] php variable scope
问题描述
我对php变量范围感到困惑. 例如:
i'm confused about the php variable scope. such as:
while(true){
$var = "yes , it is a test!";
}
printf($var)
$var
是在while语句范围内定义的,我们如何才能使其超出范围?我找不到文件的说明.
the $var
is defined in while statement scope , how could we get it outside it's scope ? and i can't find explanation on the document .
我想知道php如何处理它的作用域.
i wonder how php deal with it's scope .
推荐答案
如果您执行while(true)
,则不会过时,所以没关系.但是,如果您要表达一个真实的表情,就可以这样(这是一个无用的示例,我知道)
If you do while(true)
you will not get out of the while, so it wouldn't matter. But if you would have a real expression, something like this (this is a useless example, i know)
$i=0
while($i<10){
$var = "yes , it is a test!";
$i++;
}
printf($var);
将正常工作. 没有特殊的"while"变量作用域,printf将打印您的字符串.检查: http://php.net/manual/en/language.variables.scope .php
Will just work. There is no special "while" variable scope, the printf will print your string. check : http://php.net/manual/en/language.variables.scope.php
这篇关于php变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!