codescracker


php

PHP Variable Scope



« Previous Tutorial Next Tutorial »


You can declare your variable anywhere in the PHP script. The scope of a variable is the part of that script where the variable can be referenced or used. There are following four types of variable scopes available in PHP:

PHP Local Scope

In PHP, local variables are those variables that are declared within a function. These variables has Local scope and you can only access to these variables, within that function, where they are declared. Here is an example showing local scope of a variable in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
     $x = 50; // local scope
     echo "<p>Variable x inside function is: $x</p>";
} 
myTest();

// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>

</body>
</html>

Here is the output produced by the above PHP script:

PHP Global Local Variable

Note - You can have local variables in PHP, with same name in different functions, as local variables are only recognized by that function where they are declared.

PHP Global Scope

PHP global variable are those variables, which are declared outside the function. These variables has global scope, and you can only access to these variables, outside a function. Here is an example:

<!DOCTYPE html>
<html>
<body>

<?php
$x = 50; // global scope
  
function myTest() {
     // using x inside this function will generate an error
     echo "<p>Variable x, inside function is: $x</p>";
} 
myTest();

echo "<p>Variable x, outside function is: $x</p>";
?>

</body>
</html>

Here is the output of the above PHP script:

PHP Global Local Variables

PHP global Keyword

PHP global keyword is basically used to access the global variable from within a function. Here is an example, accessing global variables in PHP using the global keyword:

<!DOCTYPE html>
<html>
<body>

<?php
$x = 50;
$y = 100;

function myTest() {
     global $x, $y;
     $y = $x + $y;
} 

myTest(); // run function
echo $y; // output the new value for variable $y
?>

</body>
</html>

Below is the output of this PHP script:

PHP Global Keywords

PHP also stores all the global variables in the array named $GLOBALS[index]. Here, the index holds the variable's name. You can also access to these array from within functions and can be used to update the global variables directly. Here is an example. This is the rewritten script of the above PHP script:

<!DOCTYPE html>
<html>
<body>

<?php
$x = 50;
$y = 100;

function myTest() {
     $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
} 

myTest();
echo $y;
?>

</body>
</html>

Here is the output you will watch on your browser after running the above PHP script:

PHP Global Keywords

PHP static Keyword

PHP static keyword is used to hold the variable's value for further use. Here is an example, uses static keyword:

<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
     static $x = 10;
     echo $x;
     $x++;
}

myTest();
echo "<br>";
myTest();
echo "<br>";
myTest()
?>

</body>
</html>

Here is the output produced by this PHP script:

PHP Static Keyword

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test