How to define global functions in PHP

前端 未结 7 1291
陌清茗
陌清茗 2020-12-01 11:43

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

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 12:24

    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.

提交回复
热议问题