PHP Variable - Part II
PHP Variable Scope
Each variable in PHP has it own scope. PHP organizes code into functions or classes. Inside function, PHP provides another storage space for variable so two variables with the same name inside and outside function have separated storage locations or spaces. This separate storage space is known as scope of variable. Let's take a look at an example:
<?php
$x = 20;
function swap($a, $b) {
$x = $a;
$a = $b;
$b = $x;
echo $x; // $x = $b
}
swap(1, 2); // $x = $b = 2
echo $x; // 20
?>
In the above example, we define a variable $x and assign 20 to it. We have a function called swap. Inside this function we defined a variable $x to keep value of a variable when we swap 2 variable's values. Because two of these variables have different scopes so when we change value of one variable, another will not be affect.
Global variables
Global variables allow you use the variable cross the boundary between functions and other scope. To define a global variable in PHP you use the following form:
<?php
global $x = 10;
global $str = 'This is a global variable';
?>
When you use global variable you should take it into consideration because any code can change it accidently without knowing the effect.
Static variables
Static variable is not destroyed when a function ends. It means static variable does not lose its values when you call function. Static variable similars to global variable but only access by that function it defines. To define a stactic variable you use keyword static.
<?php
function count(){
static $time= 0;
$time = $time + 1;
echo $time;
}
count(); // 1
count(); // 2
count(); // 3
?>
In the above example, we define a static variable inside the function count and we call this function three times. The value of static variable $time is retained each time this function is called.
Super global variables
PHP provides a special kind of variable called super global variable. Super global variables provide information about the PHP script's environment. These variables are automatically available. Since PHP 4.0.1 super global variables are defined as array. Here are common super global variables you'll use most:
- $GLOBALS Contains any global variables that are accessible for the local script. The variable names are used to select which part of the array to access.
- $_SERVER Contains information about the web server environment.
- $_GET Contains information from GET requests (a form submission). These values should be checked before use.
- $_POST Contains information from POST requests (another type of form submission). These values should be checked before use.
- $_COOKIE Contains information from HTTP cookies.
- $_FILES Contains information from POST file uploads.
- $_ENV Contains information about the scripts environment.
- $_REQUEST Contains information from user inputs. These values should be checked before use. $_GET or $_POST should be used instead of $_REQUEST as they are more specific.
- $_SESSION Contains information from any variables registered in a session.