|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #17055 Must declare function before referencing when in the scope of an if statement
Submitted: 2002-05-06 19:38 UTC Modified: 2018-03-29 17:16 UTC
Avg. Score:3.0 ± 1.7
Reproduced:4 of 5 (80.0%)
Same Version:2 (50.0%)
Same OS:4 (100.0%)
From: spamsucks86 at comcast dot net Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 4.1.2 OS: All
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2002-05-06 19:38 UTC] spamsucks86 at comcast dot net
if (1==1) {
	if (1==2) {
		echo 'blah';

	else { test('test'); } <--RIGHT HERE

	function test ($blah) {
		echo $blah;

This script gives the error: Fatal error: Call to undefined function: test() <line 6>. 

PHP should have recognized that since the first if statement was true, all functions in the scope of that if statement should be declared. If I move the function declaration to before I use it, it works. 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2002-05-07 02:52 UTC]
This is not really a bug, as PHP only 'remembers' functions as soon as they are seen by the compiler.
Because the compiler does not know if this specific branch will be executed, it doesn't add the function to the function table immediately, but waits until it actually 'hits' the function declaration.
Therefore the function will nto be defined at the locate where you get your error.

The only way to prevent this is to move out the function declaration out of the branch and in the main scope of the script. Then it doesn't matter if you define it before or after the call to the function.
I'm making this report a suspended function/request, as I don't see any fast implementation of this.

 [2018-03-29 17:16 UTC]
-Package: Feature/Change Request +Package: Scripting Engine problem
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Sep 18 05:03:36 2021 UTC