function - PHP - Variable Scope Confusion -



function - PHP - Variable Scope Confusion -

this question has reply here:

reference: variable scope, variables accessible , “undefined variable” errors? 2 answers

i know crazy basic, little confused situation. may because mixing logic other languages bare me.

while($row = $stmt->fetch(pdo::fetch_assoc)) { $un = $row['username']; $pw = $row['passwrd']; $at = $row['account_type']; $globals['fn'] = $row['fname']; }

so code within function. variables $un, $pw , $at declared , given value within block of code.

now understanding variables declared in block of code able used in block.

as can see have $globals['fn'] variable setup utilize in other file makes sense me create global.

now question is:

how possible reference variable outside of code block when declared within block.

according article here php variable scope variables declared outside of function not same within function if share same name. need prepend variables within block global or $globals array.

i come objective-c , c# background , getting php before starts downwards voting me :-)

note duplicate - may have been asked before not uncertainty since simple. related situation in own words. helps me understand better. question links explains read in linked article. not address while loop issue referring to. in case bit different believe.

now understanding variables declared in block of code able used in block

wrong, if mean {} block.

according php manual

any variable used within function default limited local function scope.

there no mention of {} level scope within function. variable declared within function available throughout it, if declared within sub braces. that's why still available. variables $un can accessed outside loop, contain values lastly iteration.

php function variables scope global-variables

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -