PHP Variable Scope

PHP Variable scope refers to where you can use and change a variable in your code. It is used for writing PHP code that is easy to maintain and doesn’t have errors.

Previous TopicPHP Variables

In this article, we will know the different types of variable scopes in PHP  and how they control to modify their values.

Variable Scope

PHP Variables can have different scopes, which identify where the variable is accessible.

There are three main scopes of variables –

  • Local
  • Global
  • Static

Local Scope

A Local Scope variable is defined inside a function and only accessible inside that function.

This means that if you’ve declared a variable inside a regular function, its scope is limited to that function, and you cannot access its value outside of that function.

Note – Once the function execution is complete, the variable is destroyed, and its value is no longer accessible.

Example 

In this example,

  • $num that is accessed inside the displayNumber() will display output 20
  • $num that is accessed outside the displayNumber() will generate an error
function displayNumber() {
   $num = 20;
   echo $num;
}
displayNumber();  // output 20
echo $num;   // an error

Global Scope

A Global Scope variable is defined outside a function and only accessible outside that function.

This means that if you’ve declared a variable outside a regular function, its scope is limited to outside that function. So, you cannot access its value inside of that function.

Example

In this example,

  • $num that is accessed inside the displayNumber() will generate an error
  • $num that is accessed outside displayNumber() will display output 20
$num = 20;

function displayNumber() {
   echo $num;
}
displayNumber();
echo $num;

Static Scope

Normally, when a function execution is completed, all variables are destroyed. In some cases, We need to keep variables, So, to do this, we have to use the static scope variable.

Static scope is a variation of local scope. It is defined with the static keyword within a function. It mainly holds the variable values from the last calling function.

with local scope

in this example, the variable will initiate to zero at each the calling of function. So, it always displays 1.

function increaseNumberByOne() {
   $num = 0;
   $num++;
   echo $numm;
}

increaseNumberByOne() // output 1
increaseNumberByOne() // output 1
increaseNumberByOne() // output 1

With Static Scope

in this example, the variable will initiate to last values at next the calling of the function. So, it displays by increasing by 1 on each time calling function.

function increaseNumberByOne() {
   $num = 0;
   $num++;
   echo $numm;
}

increaseNumberByOne() // output 1
increaseNumberByOne() // output 2
increaseNumberByOne() // output 3

Access the Global Scope Variable inside a Function

There are various ways to access scope variables inside a function but here you learn some important ways

The global keyword

To access the global scope variable inside a function, you can use the global keyword.

The “global” keyword lets you use a variable from the global scope inside a function, but first, you need to declare it with a global keyword before you can access it.

$num = 20;
function displayNumber() {
   global $num;
   echo $num;
}
displayNumber();  // output 20

The $GLOBALS superglobal array

To access the global scope variable inside a function, you can use the $GLOBALS superglobal array.

$num = 20;
function displayNumber() {
   echo $GLOBALS['num];
}
displayNumber();  // output 20

The $_GLOBALS Associative array

To access the global scope variable inside a function, you can also use the $_GLOBALS associative array.

$num = 20;
function displayNumber() {
   echo $_GLOBALS['num];
}
displayNumber();  // output 20

This example is similar to using the $GLOBALS superglobal array. The $_GLOBALS array also holds all global variables as key-value pairs, and you can access them by specifying the variable name as an index within the array.

Pass global variables as function arguments

To access the global scope variable inside a function, you can also pass global variables as function arguments.

it is a recommended way.

$num = 20;
function displayNumber($num) {
   echo $num;
}
displayNumber($num);  // output 20

Access the Local Scope Variable Outside a Function

To access the global scope variable outside a function, you can use the $GLOBALS superglobal array.

<?php
function displayNumber() {
    $GLOBALS['num'] = 50;
}
displayNumber(); 
echo $num;  // output 50
?>

Best Practice

Instead of updating global variables within functions, return values from functions and assign them outside the function.

Avoid

$num = 20;
function displayNumber() {
   global $num;
   $num = 50;
}
displayNumber();  
echo $num; // output 50

Preferred

$num = 20;
function displayNumber($num) {
   $num = 50;
}
$num = displayNumber();  
echo $num; // output 50

Points to remember

  • Avoid using too many global variables as they can make your code hard to manage and debug.
  • Limit the scope of variables by using local variables within functions or methods.
  • Use static variables when you need a local variable to hold its value between function calls.

Suggestion

You’ve covered all the different variable scope types with simple examples, which should help you grasp the concept. To enhance your skills and write clean, error-free code, it’s essential to practice regularly.

Next Topic – PHP Data Types