From:             herb at bobbingwide dot com
Operating system: Windows XP
PHP version:      5.3.17
Package:          *Programming Data Structures
Bug Type:         Bug
Bug description:using if ( expression ) requires functions to be declared 
before calls 

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 bug report at https://bugs.php.net/bug.php?id=63156&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=63156&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=63156&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=63156&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=63156&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=63156&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=63156&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=63156&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=63156&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=63156&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=63156&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=63156&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=63156&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=63156&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63156&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=63156&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=63156&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=63156&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=63156&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=63156&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=63156&r=mysqlcfg

Reply via email to