How can I define a global function which would be accessible from any page?
Global variables refer to any variable that is defined outside of the function. Global variables can be accessed from any part of the script i.e. inside and outside of the function. So, a global variable can be declared just like other variable but it must be declared outside of function definition.
The global Keyword Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.
PHP User Defined FunctionsA function is a block of statements that can be used repeatedly in a program. A function will not execute automatically when a page loads. A function will be executed by a call to the function.
Global functions allow you to perform processing at a specific resolution and extent. By default, global functions process rasters at the source resolution and full extent. This means that applying a global function may take some time, depending on the size of the outputs.
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
in php.ini
to find the file.
In file include.php:
function myGlobalFunction() { // Do something }
Then in every page you want to use it:
include 'include.php'; myGlobalFunction();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With