php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #37776 position of define() in source matters
Submitted: 2006-06-10 23:12 UTC Modified: 2006-06-15 11:12 UTC
From: karibu at gmx dot net Assigned: colder (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: MacOS X, MorphOS
Private report: No CVE-ID: None
 [2006-06-10 23:12 UTC] karibu at gmx dot net
Description:
------------
When I *call* a function which uses a define before this define is actually defined in the source it appears in the function to be undefined. 

I didn't found this behavoir documented (not in the english or german section) but it would probably very useful as it quickly can lead to unexpected problems w/o instantly possible to detect why (in a script of mine it caused busylooping f.ex.).

Reproduce code:
---------------
<?php

printfoobar();
exit;

define(FOO, 'bar');

function printfoobar()
{
    echo FOO;
}

?>   


Expected result:
----------------
bar

Actual result:
--------------
FOO

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-06-10 23:27 UTC] brianlmoon@php.net
Would you expect this to work different somehow?

<?php

printfoobar();
exit;

$FOO = 'bar');

function printfoobar()
{
    echo $GLOBALS["FOO"];
}

?>   

I would not.  Not only did you call the function before the define, but you exit() before the define.  There is no way that is getting defined.
 [2006-06-10 23:55 UTC] karibu at gmx dot net
same happens w/o the exit too. 

anyway... of course I expect a different result in that case. It's very confusing for (e.g.) c programmers where that works just fine:


int main(void)
{
   printfoobar();

   return 0;
}


#define FOO "bar"

void printfoobar()
{
   printf(FOO);
}


So it should be documented IMHO... a small extra sentence in the documentation doesn't hurt.. I think ;)
 [2006-06-11 08:53 UTC] colder@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

I believe it can't hurt anybody.

http://patches.colder.ch/constants-1.43.patch?markup
 [2006-06-12 13:56 UTC] colder@php.net
I reopen this bug as a consequence of Sean's comment (thanks for it).

Will fix that ASAP.
 [2006-06-15 11:12 UTC] colder@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

Moved from language/constants to define()'s manual page.

I hope it's alright now (:



 [2020-02-07 06:11 UTC] phpdocbot@php.net
Automatic comment on behalf of colder
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=aa617da6c79fc01cfecec9a6d11f843211d41c70
Log: Fix #37776 (move from constants to define)
 [2020-02-07 06:11 UTC] phpdocbot@php.net
Automatic comment on behalf of colder
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=a1883180b5959837f9a4c35e3e3fa6d7a13b919e
Log: Fix #37776 (constants cannot be referenced before being defined)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 13:01:33 2025 UTC