Function - PHP - Variable Scope Confusion

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

   This question already has an answer here:
    * /questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and /questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and 3 answers

Answers

Now my understanding is that variables that are declared in a block of code are only able to be used in that block

Wrong, if you mean {} as a block.

http://us1.php.net/manual/en/language.variables.scope.php http://us1.php.net/manual/en/language.variables.scope.php

Any variable used inside a function is by default limited to the local function scope.

There is no mention of {} level scope within a function. Any variable declared inside a function is available throughout it, even if it as declared inside any sub braces. That s why its still available. Your variables like $un can be accessed even outside the loop, just that they will contain the values from last iteration.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/24360194/php-variable-scope-confusion

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils