Variables Scope in PHP



In previous php tutorial  we understood what is variable and how to declare and use variable in php.

Variable scope means the way how to declare and use variables in php script.

In PHP there are two basic variable scopes.

  • local scope
  • global scope

Variables scope variable scope comes in picture when we use function in php script.


 Local Variable scope

Local variable are initialized inside the function body.
Local scope variables can not accessed out side the function body in php.

Example of local variable

<?php

function addition()
{

$str = “meera academy”; // local scope

echo “Hello ” . $str ;
}

addition();
echo “Hi ” . $str ;

?>

The output is :
Hello meera academy
Hi Notice: Undefined variable:

In above local scope variables example we have declared variable $str with value “meera academy” inside the addition() function body.
The echo statement inside the addition() function run correctly while the other echo statement outside the function additiion() will not generate output.

We can only use $str variable inside the function addition() body, it will not work outside the function addition().


 Global variable scope

A variable declared outside the function body and it can accessed only out side of function body.

Example of global scope variable

<?php

$str = “meera academy”; // global scope
function addition()
{

echo “Hello ” . $str.””;

}

addition();
echo “Hi ” . $str ;

?>;

The output is :
Hello
Hi meera academy

In global scope the variables declared out side the function body and it can be work only the outside the function body.


 PHP global keyword and static keyword.

PHP global keyword is used to access global scope variable inside a function body by using global keyword before the variable name.

We all know that we can not use global scope variables inside the function body, but if we want to do this use the global keyword before the variables name inside function body.

Example of global keyword

<?php

$first = 5;
$second = 2;

function addition()
{
global $first, $second;
$sum = $first + $second;
echo “The sum = ” . $sum;
}

addition();

?>

The output is :
The sum = 7

we can write above example as like below :

we use $GLOBALS[] keyword to access the global scope variables inside the function body as well.

<?php

$first = 5;
$second = 2;

function addition()
{
$sum = $GLOBALS[‘first’] + $GLOBALS[‘second’];
echo “The sum = ” . $sum ;
}

addition();

?>

The output is :
The sum = 7


 The static php keyword

In php function all local variable are deleted automatically. If we want a local variable not to be deleted then use static keyword before the variable name at the time of variable declaration.

Example of static variables in php

<?php

function addition()
{

static $first=1;
echo $first;
$first = $first + 1;

}

addition();
addition();
addition();
addition();

?>

The output is : 1234

Leave a Reply

Your email address will not be published. Required fields are marked *