Edit report at https://bugs.php.net/bug.php?id=63156&edit=1
ID: 63156 Updated by: larue...@php.net Reported by: herb at bobbingwide dot com Summary: using if ( expression ) requires functions to be declared before calls -Status: Open +Status: Duplicate Type: Bug Package: *Programming Data Structures Operating System: Windows XP PHP Version: 5.3.17 Block user comment: N Private report: N New Comment: dup to #17055 Previous Comments: ------------------------------------------------------------------------ [2012-09-25 05:13:48] re...@php.net Top level declaration are available after compiling, but conditional declaration can only be available after executed. (since PHP itself didn't do optimization, i`f(true) {doSomething();}` is not equal to `doSomething();` exactly ) the same rules for class declarations. This could be classified as a documentation problem. ------------------------------------------------------------------------ [2012-09-25 02:40:07] larue...@php.net it is good manner always declare function/class before using it. ------------------------------------------------------------------------ [2012-09-24 19:56:55] herb at bobbingwide dot com Description: ------------ When enclosing code between if ( expression ) { and } it appears that versions of PHP up to and including 5.3.17 require functions to be declared before they are used. I first noticed the problem when I tried to wrap a whole PHP source file like this <?php if ( !defined( 'CONSTANT' )) { define( 'CONSTANT', true ); // whole file here } // end defined I reduced the problem to the simplest; replacing !defined( 'CONSTANT') with true and got the same unexpected results. The code did not work unless the (top level) functions were declared before they were used. I found the problem on PHP 5.3.5 where the code produced a Fatal error: Call to undefined function. I have since reproduced the Fatal error on PHP 5.3.16 and 5.3.17 If you remove the 'if test' (lines 2 and 7) the code works fine. If you put the call to the function after the declaration that also works. This code, where the call to b() is before the declaration of function b, also works <?php if ( true ) { function a() { echo "a" . PHP_EOL; b(); } function b() { echo "b" . PHP_EOL; } a(); } producing a b Test script: --------------- <?php if ( true ) { a(); function a() { echo "a" . PHP_EOL; } } Expected result: ---------------- a Actual result: -------------- Fatal error: Call to undefined function a() in C:\apache\htdocs\wordpress\wp-con tent\plugins\play\defined.php on line 3 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=63156&edit=1