Docstoc

PHP variable Scopes

Document Sample
PHP variable Scopes Powered By Docstoc
					        By
Rituparna sonowal
(Htmlwebzine.co.in)
A variable can be defined as a symbol that can store or hold different values a
different times. Whether It is a programming language or scripting language, the
concept of variables are almost similar. Like other programming language we declare
our variables in a php script and we can declare them anywhere in the script.
Declaration location of a variable is very important because accessing of that
variable is greatly influence by the location of the declaration.
This accessibility domain is known as its Scope. In PHP Hypertext Preprocessor
variables can be one of four scope types:

1.   Local variables
2.   Function parameters
3.   Static variables
4.   Global variables
variable scope: Local variables
A variables declared in a function is considered as a local variable to the function.
it can not be use outside of a function in which the local variable has been
declared. If it is used in outside of the function in which it is declared then it is
considered as an entirely different variable. when the function in which local
variable has been declared is exit then the variable and its coresponding value
are destroyed. Local variables are helpful because they eliminates the possibility
of unexepected results that can due to the intentional and unintentional
modification of globally accessible variables. An example is shown below:

 <?php
 $var = 1;
 function test(){
   $var = 0; //local variable to function test()
   echo "\$var inside function is $var.";
   echo "
 ";
 }
 test();
 echo "\$var outside function is $var.";
 ?>
variable scope: Function parameters
Like many other programming languages, Function in PHP also accepts
arguments. these arguments must declare in the function header. these
arguments accepts values that come from outside of the function. those values
are no longer accessible once the function has exited. Function parameters are
declared after the function name and inside parentheses. declaration of function
parameter is shown in the example below:

   <?php
   function multiply ($value) {
     $value = $value * 10;
     return $value;
   }
   ?>

   <?php
   $value = 4;
   $value = multiply ($value);
   echo "$value";
   ?>
variable scope: Static variables
A variable can be declared as static by placing the keyword static in front of it.
Unlike variables in function parameter ,static variables does not lose its value
when the function exits and will still hold that value if the function called again.
An example is shown below:


 <?php
 //Static Vs Local variable in Php
 //Function using Static variable:
 function static_var(){
  static $count = 0;
  $count++;
  echo $count;
  echo "
 ";
  }
  static_var();
  static_var();
  static_var();
 ?>
variable scope: Global variables
Unlike local variables Global variables can be accessed in any part of the program.
To modify a global variable it must be explicitly declared to be global in the
function in which it is to be modified. This can be done by placing the keyword
global in front of the variable that should be recognized as global. an example is
given below:

<?php
$var = 9; //variable $var is declared at global scope
function test(){
  global $var; // variable $var is declared as global.
  $var--;
  echo "value of var is $var";
}
test();
?>
PHP's superglobal variables:
PHP offer us some very useful predefined variables and moreover these
variables are accessible from anywhere within the executing script. These
variables provide us a good amount of environmental specific information. some
of these variable are:
HTTP_HOST,HTTP_USER_AGENT,HTTP_ACCEPT_LANGUAGE,HTTP_ACCEPT_ENCO
DING, PATH,SERVER_SOFTWARE etc. All the superglobal variables are shown in
the image below. To get the list of all superglobal variables write and execute the
following script:

<?php
foreach ($_SERVER as $var => $values){
  echo "$var => $values
";
}
?>

				
DOCUMENT INFO
Tags:
Stats:
views:3
posted:10/13/2012
language:
pages:7