How to define global functions in PHP

Sussagittikasusa picture Sussagittikasusa · Dec 16, 2010 · Viewed 71.5k times · Source

How can I define a global function which would be accessible from any page?

Answer

user2656037 picture user2656037 · Aug 6, 2013

If you want your function to always be available, without including it, do this:

  1. Create your function in a PHP file.

  2. 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 in php.ini to find the file.