How to define global functions in PHP

Active3 hr before
Viewed126 times

7 Answers


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 = "my_superglobal_function.php",Or if you write it with a non absolute path, like this:,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';
load more v

The above script will output 3. By declaring $a and $b global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function. ,The above example will output:, This example demonstrates that when assigning a reference to a static variable, it's not remembered when you call the &get_instance_ref() function a second time. , A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array. The previous example can be rewritten as:

object(stdClass) #1 (0) {
load more v

Accessing global variable inside function: The ways to access the global variable inside functions are:,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.,How to declare a global variable in PHP?,Using array GLOBALS[var_name]: It stores all global variables in an array called $GLOBALS[var_name]. Var_name is the name of the variable. This array is also accessible from within functions and can be used to perform operations on global variables directly.


$variable_name = data;

load more v

< ? php
$myVariable = "a";

function changeVar($newVar) {
   global $myVariable
   $myVariable = "b";
echo $myVariable; // Should echo b
load more v

So here’s a plea to framework writers in the PHP world: If we could all agree to do this for any function, defined in the global scope, which has an obvious risk of nameclashing, I think we would all be better off.,We’re still using a global symbol, but at least it’s dynamic, rather than static.,A practical guide to leading radical innovation and growth.,Write an article for us

Recently, I had a look at some code, written for CakePHP. My eye caught a function e, which is shorthand for echo. A single letter, regular function is undoubtedly the simplest way to extend PHP’s syntax. Thinking about it, it’s fairly obvious, but it just never occurred to me.
Well, the CakePHP developers made a mistake, as it should have been shorthand for echo htmlspecialchars. Nonetheless, the syntax works well. So I began using a globally defined function which looks a bit like this:

function e($string) {
   echo htmlspecialchars($string);
load more v

If you wish to use a PHP global variable inside a certain function, you should use the keyword global in front of the variable. In the example below you can see how PHP variables $x and $y are used inside a function called learnTest().,By expressing a PHP variable outside the function, you make it a PHP Global variable. This means that a certain variable can be used anywhere outside the function.,For a local variable to stay after the function is executed, a static keyword must be used when declaring it. You can see an example of how it is applied on the PHP variable $x below.,A local PHP variable scope also indicates that after a certain function is done, the variables inside are deleted. Sometimes, we might prefer to keep them longer.

< ? php
$text = "Hey coders!";
$x = 9;
$y = 1; ?
load more v

Declare Global Variable in PHP,Use the global Keyword to Declare a Global Variable in a Local Scope in PHP,Use the define() Function to Define a Constant Global Variable in PHP,Use the $GLOBALS Super Global Variable to Use the Global Variable in the Local Scope in PHP

Example Code:

#php 7. x
   ? php
$crypto = 'Bitcoin';

function body() {
   global $crypto;
   echo $crypto.
   " is a top cryptocurrency.";
body(); ?
load more v

Other "define-functions" queries related to "How to define global functions in PHP"