How PHP translate our code?
I got this code
`
//
// prints out "Hello World!"
//
hello_world(); //First call
function hello_world()
{
echo "Hello World!<br/>\n";
}
hello_world(); //second call
?>`
Bot开发者_Go百科h of 'hello_world' call will print out the same result. It's easily to understand why the second call will be output 'Hello world', but how the first call output the same where it's been call before the initiation of the function hello_world itself ?
PHP files are parsed and then run, in two separate steps. The function is parsed before it is called, which gets rid of the need for forward declarations/prototyping.
PHP functions can be called before they are defined because it is parsed and then executed.
Functions are evaluated before they are called. Since the function is in the same file, both function calls are valid and point to the function you've defined.
There are exceptions to this, if you've defined a function inside another function, or if you wrap them in conditional statements, but the code you supplied is most perfectly valid.
More Info: http://www.php.net/manual/en/functions.user-defined.php
精彩评论