- Variables Scope In PHP
- PHP Variables Scope
- Local & Global Variable Scope
- Local Scope variable Example: 
- Global Scope Variable Example: 
- Example Explanation:
- Global Keyword In PHP
- PHP Variable Scopes
- Introduction to PHP variable scopes
- Local variables
- Global variables
- Superglobal variables
- Static variables
- Function parameters
- Summary
- PHP Variables Scope
- Global and Local Scope
- Example
- Example
- PHP The global Keyword
- Example
- Example
- PHP The static Keyword
- Example
Variables Scope In PHP
Our goal in this article is to explain how PHP variables scope impacts accessibility and lifetime of variables. No matter what level of PHP developer you are, understanding PHP variables scope is a crucial aspect of writing an efficient and effective script. Let’s explore PHP variable scope.
PHP Variables Scope
Local & Global Variable Scope
Local variables in PHP are variables that are declared within a function and are only accessible within that function. Consequently, the variable can only be used and modified within the function and cannot be accessed outside of it.
Whenever a variable with local scope exits scope, which occurs when a function ends, it is destroyed and its value is lost.
Using the $ symbol followed by the variable name, you can declare a local variable in PHP.
Local Scope variable Example: 
Variable a from inside the function is: $a
«; > localVariable(); // a outside the function will generate an error echo «
Variable a from outside the function is: $a
«; ?>?php>
Global variables are variables that are declared outside of any function and are accessible from anywhere in the script. Any function can access, use, and modify a global variable without having to declare it again.
You can use global variables to store information that is used by multiple functions or throughout your script.
Global Scope Variable Example: 
Example using both Global and local variable scope:
Variable a from inside the function is: $a
«; > echo globalLocal() ; //This prints the local variable echo «
Variable a from outside the function is: $a
«; //This prints the global variable ?>?php>
Example Explanation:
In above code, a global scope variable named $a is declared with a value of 9. Then, a function named globalLocal is declared, which declares its own local scope variable named $a with a value of 11.
When the function is called, it prints the value of its local scope variable $a, which is 11. This is done by concatenating the string “Variable a from inside the function” with the value of $a using the . operator and printing the result using echo.
After the function is called, the value of the global scope variable $a is printed, which is 9. This is done by concatenating the string “Variable a from outside the function” with the value of $a using the . operator and printing the result using echo.
The final output of the code will be two paragraphs, each containing the value of a different $a variable.
Global Keyword In PHP
Using the global keyword allows you to access a global variable within a function.
You can achieve this by using the global keyword before the variable (inside the function):
PHP Variable Scopes
Summary: in this tutorial, you’ll learn about PHP variable scopes, which specify the part of code that can access a variable.
Introduction to PHP variable scopes
The scope of a variable determines which part of the code can access it. The locations where the variable can be accessible determine the scope of the variable.
In PHP, variables have four types of scopes:
Local variables
When you define a variable inside a function, you can only access that variable within the function. And it’s said that the variable is local to the function.
The following example defines the say() function that displays the ‘Hi’ message:
function say() < $message = 'Hi'; echo $message; >
Code language: PHP (php)
Inside the say() function, we define the $message variable. The $message variable is a local variable. And you cannot access it from the outside of the say() function.
Also, the $message variable only exists during the execution of the say() function. Once the say() function ends, the $mesage variable won’t exist anymore.
Global variables
When you declare a variable outside of a function, the variable is global. It means that you can access the variable anywhere within the script except inside a function. For example:
$message = 'Hello'; function say() < $message = 'Hi'; echo $message; > echo $message; // Hello
Code language: PHP (php)
In this script, we have two variables with the same name $message .
The first variable is the global variable because we define it outside of a function. The $message variable that we define inside the function is the local variable. Even though these variables have the same name, they’re two different variables.
PHP allows you to access a global variable within a function by using the global keyword. For example:
$message = 'Hello'; function say() < global $message; echo $message; // Hello > say();
Code language: PHP (php)
- First, define a global variable called $message .
- Second, reference the global variable $message inside the say() function.
It’s important to note that it’s not a good practice to use global variables.
Superglobal variables
PHP has a list of built-in variables, which are known as superglobal variables. The superglobal variables provide information about the PHP script’s environment.
The superglobal variables are always available in all parts of the script. The following table shows the list of PHP superglobal variables:
Superglobal Variables | Meaning |
---|---|
$GLOBALS | Returns an array that contains global variables. The variable names are used to select which part of the array to access. |
$_SERVER | Returns data about the webserver environment. |
$_GET | Return data from GET requests. |
$_POST | Return data from POST requests. |
$_COOKIE | Return data from HTTP cookies |
$_FILES | Return data from POST file uploads. |
$_ENV | Return information about the script’s environment. |
$_REQUEST | Return data from the HTTP request |
$_SESSION | Return variables registered in a session |
Static variables
A static variable retains its value between function calls. Also, a static variable is only accessible inside the function. To define a static variable, you use the static keyword. For example:
function get_counter() < static $counter = 1; return $counter++; > echo get_counter() . '
'; // 1 echo get_counter() . '
'; // 2 echo get_counter() . '
'; // 3
Code language: PHP (php)
1 2 3
Code language: plaintext (plaintext)
- First, define the get_counter() function with a static variable named $counter .
- Second, call the set_counter() function three times. As you notice that the value of the $counter variable is increased by one after each function call.
Function parameters
Function parameters are local to the function. Therefore, function parameters can only be accessible inside the function. For example:
function sum($items) < $total = 0; foreach($items as $item) < $total += $item; >return $total; > // $items cannot be accessible here echo sum([10,20,30]);
Code language: PHP (php)
In this example, the $items is the parameter of the sum() function. It can only be accessible within the sum() function.
Summary
PHP Variables Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
Global and Local Scope
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:
Example
Variable with global scope:
function myTest() // using x inside this function will generate an error
echo «
Variable x inside function is: $x
«;
>
myTest();
echo «
Variable x outside function is: $x
«;
?>
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:
Example
Variable with local scope:
function myTest() $x = 5; // local scope
echo «
Variable x inside function is: $x
«;
>
myTest();
?php
// using x outside the function will generate an error
echo «
Variable x outside function is: $x
«;
?>
You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.
PHP The global Keyword
The global keyword is used to access a global variable from within a function.
To do this, use the global keyword before the variables (inside the function):
Example
function myTest() global $x, $y;
$y = $x + $y;
>
myTest();
echo $y; // outputs 15
?>
PHP also stores all global variables in an array called $GLOBALS[index] . The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.
The example above can be rewritten like this:
Example
function myTest() $GLOBALS[‘y’] = $GLOBALS[‘x’] + $GLOBALS[‘y’];
>
myTest();
echo $y; // outputs 15
?>
PHP The static Keyword
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
To do this, use the static keyword when you first declare the variable:
Example
Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
Note: The variable is still local to the function.