PHP Function Call Placement

后端 未结 5 609
孤独总比滥情好
孤独总比滥情好 2020-12-17 21:08

Consider this snippet:

function f() {
    return \'hi\';
}

echo f();

Vs this snippet:

echo f();

function f() {
    return         


        
5条回答
  •  别那么骄傲
    2020-12-17 21:27

    compiler steps are like so:

    • Converts a sequence of characters into tokens
    • Analyses the tokens to determine there Grammatical structure.
    • Generates byte code depending on the outcome of the analyses

    So the easiest way to understand this is just because the script is not multi threaded does not mean its processed in one in line execution.

    PHP Reads your entire source code into tokens before its executed, there for it has control over the order of tokens should be executed first.

    Take this example

    while(true)
    {
        print '*';
    }
    

    Each line is a sequence of characters, so PHP Would interpret this as

    if          #T_IF
                #T_WHITESPACE
    (
                #T_WHITESPACE
    true        #T_STRING
                #T_WHITESPACE
    )
                #T_WHITESPACE
    {
                #T_WHITESPACE
    print       #T_PRINT
                #T_WHITESPACE
    '*';        #T_CONSTANT_ESCAPED_STRING
                #T_WHITESPACE
    }
    

    but just because its been read does not mean its been executed.

    So that functions are at the top of the list, this way you can execute them because there already within the systems memory.

    I believe that the reason for this is that PHP's native library such as PFO,mysql_connect functions and classes are loaded first, and they move all user defined scopes to be loaded after there native implementations.

    there loaded at the beginning of execution.

提交回复
热议问题