note 32868 rejected from language.functions by didou
| From: | didou@php.net | Date: | Mon, 22 Jan 2007 07:42:18 +0000 |
| Subject: | note 32868 rejected from language.functions by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-122031@lists.php.net to get a copy of this message | ||
Note Submitter: danvk at rice dot edu
----
While PHP does allow you to define a function within another function, this feature doesn't
work in at all the same way that it does in most other languages. The most common use of an inner
subroutine that I've seen is to define a helper function which can't be seen from outside,
and still has access to the outer subroutine's local variables. Like this:
function a(){
function b(){
print $localVariable;
}
$localVariable = 10;
b();
}
In PHP, this will cause all sorts of problems. Calling a() won't print anything, since
$localVariable hasn't been defined within b(), even though it is defined within a(). And
calling a() twice will result in an error that you're trying to redefine b().