Tutorials Point

  Learning PHP
  Advanced PHP
  PHP Function Reference
  PHP Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint

  Home     References     Discussion Forums     About TP  

PHP Local Variables


Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types:

  • Local variables

  • Function parameters

  • Global variables

  • Static variables.

Local Variables:

A variable declared in a function is considered local; that is, it can be referenced solely in that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function:

$x = 4;
function assignx () {
$x = 0;
print "\$x inside function is $x. 
"; } assignx(); print "\$x outside of function is $x.
"; ?>

This will produce following result.

$x inside function is 0.
$x outside of function is 4.

previous Printer Friendly