php variable scope

i'm confused about the php variable scope. such as:

while(true){
    $var = "yes , it is a test!";
  }
  printf($var)

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 .

i wonder how php deal with it's scope .


while is not a function. scope of variable refers to variable inside functions and classes


In PHP, a while loop doesn't create a new scope. So it will be available in the function


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);

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


Loop does not have any scope in PHP. variable is simply available outside the loop.

just echo outside the loop;

echo $var;