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
- Global
- Static
- Function parameters
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:
<?php
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:
<?php
$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:
<?php
$message = 'Hello';
function say()
{
global $message;
echo $message; // Hello
}
say();
Code language: PHP (php)
How it works.
- First, define a global variable called
$message
. - Second, reference the global variable
$message
inside thesay()
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:
<?php
function get_counter() {
static $counter = 1;
return $counter++;
}
echo get_counter() . '<br>'; // 1
echo get_counter() . '<br>'; // 2
echo get_counter() . '<br>'; // 3
Code language: PHP (php)
Output:
1
2
3
Code language: plaintext (plaintext)
How it works.
- 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:
<?php
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 has four types of variable scopes including local, global, static, and function parameters.