29

In the script below, does the order in which items are declared matter?

For example, if the add_action points to a function that has not yet been defined? Does it matter or should the function declaration always precede any code in which its called?

add_action('load-categories.php', 'my_admin_init');
function my_admin_init(){
//do something
}
Jon Onstott
  • 13,499
  • 16
  • 80
  • 133
Scott B
  • 38,833
  • 65
  • 160
  • 266

7 Answers7

35

That doesn't matter if the function is declared before or after the call but the function should be there in the script and should be loaded in.

This is the first method and it will work:

some_func($a,$b);

function some_func($a,$b)
{
    echo 'Called';
}

This is the second method and will also work:

function some_func($a,$b)
{
    echo 'Called';
}

some_func($a,$b);
totymedli
  • 29,531
  • 22
  • 131
  • 165
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
11

From the PHP manual:

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

However, while this is more of a personal preference, I would highly recommend including all the functions you actually use in an external functions.php file then using a require_once() or include_once() (depending on tastes) at the very top of your main PHP file. This makes more logical sense -- if someone else is reading your code, it is blindingly obvious that you are using custom functions and they are located in functions.php. Saves a lot of guesswork IMO.

Javaish
  • 179
  • 14
Jeremy Conley
  • 924
  • 5
  • 6
4

you can call a function before it's defined, the file is first parsed and then executed.

Headshota
  • 21,021
  • 11
  • 61
  • 82
2

No.
It is not C :P...
As you can see here , the whole file is first being parsed and then executed.
If a function that doesn't exist is being called, php will throw an error.

Fatal error: Call to undefined function

Blauharley
  • 4,186
  • 6
  • 28
  • 47
gion_13
  • 41,171
  • 10
  • 96
  • 108
1

As per my personal experience, In some special cases (Like, passing array's in function or function inside a function and so on). It's best option to define the function above the call. Because of this sometimes neither function works nor PHP throw an error.

In normal php functions, it doesn't matter. You can use both of the types.

Hariom
  • 13
  • 3
  • 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. – MaXi32 Jan 02 '22 at 09:38
0

It does not matter, as long as it is declared somewhere on the page.

as seen here:

http://codepad.org/aYbO7TYh

Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
0

Quoting the User-defined functions section of the manual :

Functions need not be defined before they are referenced, except when a function is conditionally defined

So, basically : you can call a function before its definition is written -- but, of course, PHP must be able to see that definition, when try to call it.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663