|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 20 07:00:01 2025 UTC |
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.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 ;)