News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

PHP Lesson 14 - variable scope and global declaration

Started by Parham, September 23, 2003, 09:03:36 AM

Previous topic - Next topic

Parham

This is a VERY basic introduction to scope; I neglected to mention a lot of things when writing this particular tutorial to try to keep things as simple as possible.

In PHP there is something called scope, which relates to variables.  When you declare variables, you declare them in a particular scope.  The scope is the area in which the variable is declared.  In PHP, you set your scope with curly braces (the { and } characters).  There are two particular types of scope, the global scope which refers to everything outside functions, and the local scope which refers to everything inside functions.


<?
//this is the global scope
function something() {
  //this is the local scope
}
?>


Variables you declare in the global scope cannot be used in the local scope, and variables declared in the local scope cannot be used in the global scope.  For example:


<?
//this is the global scope
$variable = 'Someone'; //declare a variable
something(); //call a function which SHOULD print out the variable
function something() {
  //this is the local scope
  print $variable; //print out the variable we set in the global scope
}
?>


The above code will print nothing.  We declared a variable in the global scope, and tried to use it in the local scope.  Of course what we could have done was pass the variable in through an argument:


<?
//this is the global scope
$variable = 'Someone'; //declare a variable
//call a function passing the variable to it which SHOULD print out the variable
something($variable);
function something($argument) { //get the argument passed into the function
  //this is the local scope
  print $argument; //print out the variable we got through the argument
}
?>


The above will print what we expect it to.  But what if we don't want to constantly pass variables inside?  Well you can always get your function to refer to the variable you want to use in the local scope from the global scope by declaring them as "global".  That may be hard to understand, so here is an example:


<?
//this is the global scope
$variable = 'Someone'; //declare a variable
something(); //call a function which SHOULD print out the variable
function something() {
  //this is the local scope
  //declare $variable as global;
  global $variable; //THIS LINE IS IMPORTANT
  print $variable; //print out the variable we set outside of this scope
}
?>


The above code will print what we expect it to ("Someone").  By declaring $variable as global inside our function (in the local scope), all references to that variable will refer to $variable in the global scope.  This means that if you declare variables in the global scope, you have one of two basic methods to use those variables inside functions: pass them in as arguments to your function, or declare them as global inside the function.  The superglobals ($_*) variables we learned about earlier, in lesson 11, bypass the scope rule.  Superglobals can be used ANYWHERE without having to declare them as global.  This is good because if a person for example submits information via the "POST" method, you won't have to constant declare them as global inside functions to use them.  You can ALSO use the $GLOBALS array to refer to variables in the global scope:


<?
//this is the global scope
$variable = 'Someone'; //declare a variable
something(); //call a function which SHOULD print out the variable
function something() {
  //this is the local scope
  print $GLOBALS['variable']; //print out the variable we set in the global scope
}
?>


Remember that the local scope includes all functions, and the global scope includes everything that isn't inside functions.  Read about it more (with more examples, and more methods) at http://www.php.net/manual/en/language.variables.scope.php

Advertisement: