PHP Scope Variables

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:

  1. Local
  2. Global
  3. Static
  4. 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.

 

function welcome()
{
	$message = 'Hi';
	echo $message;
}

 

Inside the welcome() function, we define the $message variable. The $message variable is a local variable. And you cannot access it from the outside of the welcome() function.

Also, the $message variable only exists during the execution of the welcome() function. Once the welcome() 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 = 'Welcome';

function welcome()
{
	$message = 'Hi';
	echo $message;
}

echo $message; // Welcome

 

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.

 

Using $GLOBALS instead of global

Another way to use the global variable inside the function is predefined $GLOBALS array.

 

<?php  
    $num1 = 7;      //global variable  
    $num2 = 13;     //global variable  
    function global_variable()  
    {  
            $sum = $GLOBALS['num1'] + $GLOBALS['num2'];  
            echo "Sum of global variables is: " .$sum;  
    }  
    global_variable();  
?> 
 
Output: Sum of global variables is: 20

 

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.

 

<?php

function get_count() {
    static $count = 1;
    return $count++;
}

echo get_count() .  '<br>'; // 1
echo get_count() .  '<br>'; // 2
echo get_count() .  '<br>'; // 3

?>

Output:

1
2
3

 

How it works

First, define the get_count() function with a static variable named $count
Second, call the get_count() function three times. As you notice that the value of the $count 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 final($numbers) {
    $total = 0;
    foreach($numbers as $number) {
        $total += $number;
    }
    return $total;
}

echo final([5,10,15]); // $items cannot be accessible here

?>

 

In this example, the $numbers is the parameter of the final() function. It can only be accessible within the final() function.