Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Function Call Placement

Tags:

php

Consider this snippet:

function f() {
    return 'hi';
}

echo f();

Vs this snippet:

echo f();

function f() {
    return 'hi';
}

When I run the scripts, they both produce the same results. That's great.

But my question is (and I could not find a good answer or reference in any of my searches), is it okay to call the function before it is defined (ie, from a sequential parsing of the script file perspective)?

I don't want to run into any issues or deprecation down the road if I leave the function call ahead of the function definition block in my script file.

like image 337
H. Ferrence Avatar asked Dec 06 '10 15:12

H. Ferrence


People also ask

Does function order matter in PHP?

In normal php functions, it doesn't matter. You can use both of the types. I like this answer, function definition must appear as a best practice it is like a tool that is ready to be called and you can call it later. Of course, it does not matter but for readability, function definition/declaration should go first.

What is PHP call function?

A function is a self-contained block of code that performs a specific task. PHP has a huge collection of internal or built-in functions that you can call directly within your PHP scripts to perform a specific task, like gettype() , print_r() , var_dump , etc.

What is PHP function with examples?

A function is a piece of code that takes another input in the form of a parameter, processes it, and then returns a value. A PHP Function feature is a piece of code that can be used over and over again and accepts argument lists as input, and returns a value. PHP comes with thousands of built-in features.

What is the valid way to start a PHP function?

Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$ .


2 Answers

From the Manual:

Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.

The possibility to call (reference) a function before it is defined is a PHP intentional feature and I don't think you need to worry about it becoming deprecated.

As an observation, if you can choose from declaring the function before or after, it would be common sense to declare it before it's used.

Note: The following code will give a fatal error because the function will only be defined at run rime.

<?php
echo helloWorld();
if(1){
    function helloWorld() {
        return 'hello world';
    }
}
?>
like image 170
Alin Purcaru Avatar answered Oct 20 '22 13:10

Alin Purcaru


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.

like image 23
RobertPitt Avatar answered Oct 20 '22 12:10

RobertPitt