Global Variables in PHP with Example
Global variables in PHP are those variables which are declared outside of any function and accessible throughout the program.
This means that you can access global variables anywhere, inside and outside the functions. In simple words, global variables can be accessed from anywhere inside the PHP program.
When a program begins execution, memory is allocated for global variables. These variables remain in memory throughout the entire runtime of the program.
Once the program finishes execution normally, the memory allocated to global variables is deallocated, freeing up the resources. Global variables maintain their values and state throughout the program’s execution, making them accessible from any part of the code until the program ends.
You can use the global variables when you need to share the data between multiple parts of the program in an application. This is because global variables are accessible from any part of the script in an application.
Syntax to Declare Global Variables in PHP
In PHP, global variables are typically declared outside of any function or class. The general syntax is as follows:
$variableName = value;
To access global variable once declared, you do not need to declare the variable to be global. However, to access the global variable inside the block of a function, you must explicitly declare it as global within that function using the GLOBAL or global keyword.
Let’s understand it with the help of an example.
Example 1:
<?php
// Declare a global variable
$globalVar = "I am a global variable!";
function displayGlobalVar() {
// Declare the variable as global using global keyword within the function
global $globalVar;
// Accessing the global variable from inside the function.
echo $globalVar;
}
displayGlobalVar();
?>
Output: I am a global variable!
In this example, we have defined a global variable named $globalVar outside the function. This variable holds a string value “I am a global variable!”. Inside the function displayGlobalVar(), we have used the global keyword before the variable to access this global variable. You can access or manipulate this global variable from anywhere in the program.
Advanced Examples of Global Variables in PHP
Let’s take some advanced example programs based on the global variables in PHP.
Example 2:
<?php
// Declare global variables.
$x = 20;
$y = 10;
// Define functions.
function add(){
global $x;
global $y;
$z = $x + $y;
echo "Sum: ", $z;
}
function sub(){
GLOBAL $x;
GLOBAL $y;
$z = $x - $y;
echo "Sub: ", $z;
}
// Accessing global variables from outside the functions.
echo "x = ", $x;
echo "\n";
echo "y = ", $y;
echo "\n";
// Calling functions.
add();
echo "\n";
sub();
?>
Output: x = 20 y = 10 Sum: 30 Sub: 10
In this example, we have defined two global variables named $x and $y, respectively. Then, we have defined two functions named add() and sub(). Inside the add() function, we have calculated the sum of two numbers and displayed the result using echo statement on the console.
Similarly, inside the sub() function, we have calculated the subtraction of two numbers and displayed the result using echo statement. After that, we have accessed the global variables from the outside the functions, and displayed the values of variables named $x and $y, respectively. At last, we have called the functions.
Let’s take an example in which we will define a local variable in a function with the same name of global variable.
Example 3:
<?php
// Declare global variable.
$str = "I am a global variable!";
// Define functions.
function showMe(){
$str = "I am a local variable!";
echo $str;
}
// Calling functions.
showMe();
echo "\n";
echo $str; // Accessing global variable.
?>
Output: I am a local variable! I am a global variable!
It is recommended to avoid using the same variable names for local and global variables.
Example 4:
<?php
// Declare global variable.
$str = "I am a global variable!";
// Define functions.
function showMe() {
global $str;
$str = "I am a local variable!";
echo $str;
}
// Calling functions.
showMe();
echo "\n";
echo $str;
?>
Output: I am a local variable! I am a local variable!
In this example, we have defined a global variable named $str and initialized with the value “I am a global variable!”. This variable is accessible throughout the entire script.
Inside the showMe() function, we have accessed the global variable by declaring it as global using global keyword. Then, we have modified the value of $str to “I am a local variable!” inside the function. The first echo statement inside the function prints this modified value.
The second echo statement prints the current value of the global variable $str. Since it was updated inside the showMe() function, it now outputs “I am a local variable!”.
Example 5:
<?php
$x = 20; $y = 50;
function globVar(){
global $x, $y;
$x = $x + $y;
}
globVar();
echo $x;
?>
Output: 70
Difference between Local Variable and Global Variable
Here’s a comparison between local variables and global variables in PHP in tabular form:
Aspect | Local Variable | Global Variable |
---|---|---|
Scope | Accessible only within the function or block where it is defined. | Accessible throughout the entire script, including outside functions. |
Declaration | Declared within a function or block. | Declared outside of all functions or blocks. |
Access Inside Function | Automatically accessible within the function where it is declared. | Requires the global keyword to be accessed within a function. |
Lifetime | Exists only during the execution of the function or block. | Exists during the execution of the PHP script. |
Default Behavior | Automatically destroyed once the function exits. | Retains its value across multiple function calls unless explicitly modified. |
Keyword Requirement | No special keyword is needed. | Must use the global keyword or $GLOBALS array to access within functions. |
Example | function example() {
| $globalVar = "Global";
|