note 20407 added to language.variables.scope

From: Date: Wed, 03 Apr 2002 01:11:47 +0000
Subject: note 20407 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-28523@lists.php.net to get a copy of this message
Not sure of the implications of this but... You can create nested functions within functions but you must make sure they aren't defined twice, e.g.: function norm($a, $b) { static $first_time = true; if ($first_time) { function square($x) { return $x * $x; } $first_time = false; } return sqrt(square($a) + square($b)); } print square(5); // error, not defined yet print norm(5,4); print "<br>"; print norm(3,2); print square(5); // OK If you don't include the if ($first_time) you get an error saying you can't define square() twice. Note that square is not local to the function it just appears there. The last line successfully accesses square in the page scope. This is not terribly useful, but interesting. -- http://www.php.net/manual/en/language.variables.scope.php http://master.php.net/manage/user-notes.php?action=edit+20407 http://master.php.net/manage/user-notes.php?action=delete+20407 http://master.php.net/manage/user-notes.php?action=reject+20407

« previous php.notes (#28523) next »