note 21149 added to functions

From: Date: Tue, 30 Apr 2002 23:49:15 +0000
Subject: note 21149 added to functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-30031@lists.php.net to get a copy of this message
The documentation statement: "In PHP 3, functions must be defined before they are referenced. No such requirement exists in PHP 4." is not entirely accurate (or at least misleading). Consider: function a() { function b() { echo 'I am b'; } echo 'I am a'; } You MUST call a() before you can even think about using b(). Why? The parser hasn't touched the scope inside function a (for efficiency reasons), so b has yet to be defined or even *declared*. The documentation (I believe) refers to this behaviour: a(); function a() { echo 'I am a'; } which is perfectly valid and runs as you probably expect. However, the following does not work as expected (or documented): function a() { b(); function b() { echo 'I am b'; } echo 'I am a'; } a(); Rather than getting "I am b" followed by "I am a" you get a parse error ("Call to undefined function b()"). So, the bottom line: Gewaehrleistungsausschluss -- http://www.php.net/manual/en/functions.php http://master.php.net/manage/user-notes.php?action=edit+21149 http://master.php.net/manage/user-notes.php?action=delete+21149 http://master.php.net/manage/user-notes.php?action=reject+21149

« previous php.notes (#30031) next »