How can I define a global function which would be accessible from any page?
7 Answers
If you want your function to always be available, without including it, do this:
Create your function in a PHP file.
In your php.ini file, search for the option
auto_prepend_file
and add your PHP file to that line, like this:`auto_prepend_file = "/path/to/my_superglobal_function.php"`
Or if you write it with a non absolute path, like this:
auto_prepend_file = "my_superglobal_function.php"
It will look in your
include_path
inphp.ini
to find the file.

- 30,738
- 21
- 105
- 131

- 609
- 5
- 4
-
1This should be the accepted answer, cause it solves the problem for **any page**. – isaias-b Jan 06 '16 at 10:02
-
7This is not the best solution cause you (or other people, who cannot even know about that) have to modify every environment in which your application is used – Thelambofgoat Sep 23 '16 at 08:33
-
@Lambrusco this does solve the question though; a function that is included somewhere within your own code would not truely be global, would it? – Berry M. Oct 11 '16 at 09:41
-
it _is_ a great solution if you have a lot of functions you use in all projects, like mysql things or stuff like this, or you improve/overwrite some existing funtions. If you want to share your work you can include_once the function in all cases. – Jan 24 '22 at 10:54
-
@Thelambofgoat Your objection is not supportive, because it also works with htaccess – Jan 24 '22 at 11:17
You could declare a function inside a function. Be careful to call the outside function only once or you'll get an error.
class MyClass {
function declareGlobalsFn () {
// Functions declared inside a function have global scope
function globalfn1() {echo "fn1";}
function globalfn2() {echo "fn2";}
}
}
$ob = new MyClass();
$ob->declareGlobalsFn();
globalfn1(); // fn1
globalfn2(); // fn2

- 30,738
- 21
- 105
- 131

- 528
- 4
- 7
-
3wow, this is for me a new way of creating functions. It looks like a good question for a PHP exam. Will this code execute or not? Are this closures or not? :-) Can you tell me when this is a best practice? As far as I can see perform include or require the same function, but the latter two are imho better for maintenance. Anyhow, for the good or the worse, you learned me something. That is why I upvoted your answer. Although none of you really answered the question. That answer is simply: all functions that are not class members have global scope. – Loek Bergman Jul 07 '13 at 06:30
-
I don't see any good reason for defining functions this way. Among other things, `declareGlobalsFn()` can't be called twice, or you get: `PHP Fatal error: Cannot redeclare globalfn1()` – x-yuri Sep 23 '14 at 17:45
-
2That's also new to me. There is one very good reason: you can combine declaration with inheritance by `calling parent::declareGlobalsFn()` and avoid an error by [checking for existence](http://php.net/manual/de/function.function-exists.php) first. – Robert Dec 16 '14 at 12:10
-
@x-yuri these local functions can be used to optimize a non-tail recursive function into a tail recursive one. However, it seems that php doesn't perform tail call optimisations as opposed [here](https://www.moreofless.co.uk/tail-call-optimization-elimination-php/) and [here](http://stackoverflow.com/questions/6171807/does-php-optimize-tail-recursion), so it won't really help here. However, it is possible to chop down a very long method into smaller well named digestible peaces. With these named peaces of code it should lead to a (still big) function with higher readability and less comments. – isaias-b Jan 06 '16 at 10:00
-
-
5You can prevent duplicate declaration using `if(!function_exists('globalfn1')){ function globalfn1(){ ... }}` – Neils Aug 15 '16 at 11:59
-
You could also prevent duplicate declarations by making the class a [Singleton](http://stackoverflow.com/questions/203336/creating-the-singleton-design-pattern-in-php5). – Katrina Apr 26 '17 at 15:52
-
Prevent duplicate by checking if the function is already defined and returning early if so. This actually looks pretty useful to me as something to have in a library. Putting global functions that people might not want in the root namespace is sort of rude, but it isn't possible to namespace free functions in PHP. If I include a file with MyClass then I'd have the option to call declareGlobalsFn only if I want to. – bdsl Mar 21 '18 at 22:17
-
What if `MyClass` is in a namespace? Can I still define functions in the global scope? I tried but couldn't figure out how. – Olle Härstedt Oct 12 '20 at 21:51
-
In file include.php:
function myGlobalFunction() {
// Do something
}
Then in every page you want to use it:
include 'include.php';
myGlobalFunction();

- 30,738
- 21
- 105
- 131

- 5,846
- 10
- 40
- 41
-
13If function has to be _include_-ed every time it's needed then this does not answer question. The only thing global about _myGlobalFunction_ is it's name. – Bad Loser Jul 10 '19 at 08:37
To expand on luarwo's answer, you can declare the function right in your class constructor. This could make your class a sort of function library, where the functions are accessible from any page where you create your class instance.
Sandbox\MyGameLib
<?php
namespace Sandbox;
class MyGameLib {
public function __construct() {
if (!function_exists('loveGame')) {
function loveGame($game) {
print "The game $game is awesome";
}
}
}
}
Seesaw
<?php
namespace Seesaw;
use Sandbox\MyGameLib;
$m = new MyGameLib();
loveGame('Tempest');
The game Tempest is awesome
-
4This is actually wrong since the function will not be global but part of the `Sandbox` namespace. – andig Oct 26 '17 at 08:02
-
1This needs some clarification. This is a better way of doing what OP _probably_ wants. I am assuming that OP, not knowing how `include` works, means accessible from anywhere and not necessarily, literally, in the global namespace. – istepaniuk Feb 19 '21 at 20:40
Then in every page you want to use it:
include 'include.php'; myGlobalFunction();
-
Put it in an include, then include it.
This technically may not be correct, depending on the context.
'Page' could be perceived as 'file'. For example, "You must include the function's file within each file you want to use the function".
Once a function is defined in your program, it can be accessed from anywhere moving forward up until the program has finished executing.
Say you have this:
index.php:
<?php
function echo_1() {
echo 1;
}
echo_1();
require 'boot.php';
boot.php
<?php
include_once 'page.php';
echo_1();
echo_9342949();
page.php
<?php
function echo_9342949() {
echo 9342949;
}
echo_1();
With that, your output would be 1119342949
.
Of course, when you say 'page' you may literally mean a directly accessed stand-alone 'page file', in which case the answers from the other users will suffice. However, if you're looking to use the same function from different locations throughout your program, simply define it before you intend to use it and you can access it anywhere moving forward regardless of scope.
However, if you're looking to use the same function from different locations throughout your program, simply define it before you intend to use it and you can access it anywhere moving forward regardless of scope.
This of course isn't true for things like class functions, but for normal functions this remains true.

- 30,738
- 21
- 105
- 131

- 1,547
- 1
- 18
- 23
I would suggest PHP Composer with autoloading. You can take a look at the laravel implementation for a helper function.
In brief, Just define your helper function script in the autoloading section and the PHP composer will take care of it.
Note: Do not forget to include the autoload.php file at the top level of your project.

- 1,469
- 2
- 17
- 27
-
1A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – 4b0 May 30 '18 at 04:37