Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php variable scope

Tags:

scope

php

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 .

like image 917
mike Avatar asked Apr 12 '11 07:04

mike


2 Answers

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

like image 165
Santosh Linkha Avatar answered Oct 12 '22 00:10

Santosh Linkha


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

like image 42
Ikke Avatar answered Oct 12 '22 01:10

Ikke