Scope of variables in PHP

Spread the love

Scope of variables in PHP is termed as the access level of a variable within the code. There are four types of scopes present in PHP for variables.

 

  1. Local variable
  2. Global variable
  3. Static variable
  4. Function parameters

Local Variables:

Lets see local variable first in a function.

function num1()
{
$num1 = 100;
echo "Number -".$num1;
}

Here $num1 is local variable for the function num1(). If you try to access the value of $num1 which is 100,  outside the function then it will print blank.

Explanation: In function num1() we assigned 100 to $num1 inside the function. Hence the scope of $num1 becomes local to the function num1, value of $num1 exists only withing the function num1() and not visible outside it.

Let’s see another example.

$num1 = 200;

function num1()
{
echo "Number-".$num1;
}

Here the output of $num1 will be blank. As we have assigned value 200 to $num1 outside the function. Inside the function, $num1 will become local variable to the function. And the value assigned outside to the variable $num1 makes $num1 as a global assignment. This thing will get clearer when you see the below description about global variables.

[ad name=”Responsive Text”]

 

Global variables:

Let’s see global variables under Scope of variables in PHP. Consider the same example:

$num1 = 200;
num1();

function num1()
{
echo "Number-".$num1;
}

As seen earlier output of above function is blank value. What if you want to print the $num1 value from the inside of the function, i.e. 200? This can be achieved by changing the scope of variable $num1 to GLOBAL.

We can do this by writing “global” keyword before $num1 and redeclaring $num1 inside the function or wherever we want to use.

So let’s update the above example:

$num1 = 200;
num1();

function num1()
{
global $num1;
echo "Number-".$num1;
}

Output – Number-200

We can also achieve same global functionality using $GLOBALS – a superglobal variable:

$num1 = 200;
num1();

function num1()
{

echo "Number-".$GLOBALS['num1'];
}

Output – Number-200

 

Static Variables:

Another type of variable under Scope of variables in PHP is “Static variable“. Static variables are those variables which can retain their values in multiple function calls. As the name suggests, the value present in these type of variable is static and will not lose even with different function calls.

We can declare the static variable by including “static” keyword before the variable name.

Example:

function visitor_count()
{
static $counter = 0;
$counter++;
echo "Visitor count = ".$counter."<br>";
}

for($i=0;$<3;$i++)
{
visitor_count();
}

Output:

Visitor count = 1
Visitor count = 2
Visitor count = 3

As you see we have initialized the value of the $counter to zero at first. Then inside the loop, we have called the same function three times. In each call, the value of the $counter is increasing. Here the first assignment to $counter i.e. zero is not lost and it retains for each call.

Function Parameters:

As we have already seen in the previous tutorial of “Functions in PHP”, function parameters are the arguments present in circular brackets after the function name.

Example:

function test($name, $age)
{
echo "Name = ".$name. " Age = " . $age;
}

You can see “Functions in PHP” for more details about function parameters.


Spread the love

Leave a Comment