How to define global functions in PHP

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.

Leave a Comment