3.7 Variables and Scope


Variables cannot be "seen" in the entire application. This is a very important issue because otherwise programmers would easily run into trouble. Especially when building huge applications, it is important to know where which variables can be seen and used.

Let's start with a simple example:

 <?php         $a = "Hello World";         echo "main: $a<br>\n";         display_data(); function display_data() {         echo "display data: $a<br>\n"; } ?> 

First, $a is initialized and displayed on the screen. Then display_data is called, which tries to display $a again:

 main: Hello World display data: 

As you can see, display_data displays nothing because $a cannot be seen by the function. $a has been initialized in the main function and therefore it is not visible for functions called by the main function.

However, if you have to access $a in display_data, it has to be declared as global:

 <?php         $a = "Hello World";         echo "main: $a<br>\n";         display_data();         echo "main: $a<br>\n"; function display_data() {         global $a;         echo "display data: $a<br>\n";         $a = "Hello Frank"; } ?> 

With the help of the keyword global, it is possible to read and modify any global variable:

 main: Hello World display data: Hello World main: Hello Frank 

As you can see, the content of $a can be modified by display_data.

Another way to access $a is to use an array called $GLOBALS:

 <?php         include("inc/vars.php");         echo "main: $a<br>\n";         display_data(); function display_data() {         echo "display data:".$GLOBALS["a"]; } ?> 

With the help of $GLOBALS, it is possible to access any variable that has been defined globally. When executing the script, the output will be displayed on the screen:

 main: Hello World display data:Hello World 

Another important thing, especially when dealing with huge projects, is to include variables using a command called include. With the help of include, files containing functions and variables can be added to a PHP script and will be visible for the interpreter. In the next example you will see a file called vars.php, which will be included in the script.php. Let's get to vars.php first:

 <?php         # Defining a string         $a = "Hello World"; ?> 

As you can see, the file contains only the definition of $a. In script.php vars.php is included, so $a can be accessed as if it were defined in script.php.

 <?php         include("inc/vars.php");         echo "main: $a<br>\n";         display_data(); function display_data() {         global $a;         echo "display data: $a<br>\n"; } ?> 

It is recommended to put files that contain only definitions of variables in a separate directory so that these files cannot be mixed up with files containing other source code.

When executing the script, you will see that the result will be the same as in the previous example:

 main: Hello World display data: Hello World 

Including one file is simple, but what happens when the file included in script.php contains included files? Let's have a look at the code of script.php.

 <?php         include ("inc/vars.php");         echo "$a<br>";         echo "$b"; ?> 

This time vars.php does not contain the definition of the variables but includes two files containing the definition of $a and $b.

 <?php         include ("inc/base_a.php");         include ("inc/base_b.php"); ?> 

$a is defined in base_a.php:

 <?php         $a = "Hello World"; ?> 

$b is defined in base_b.php:

 <?php         $b = "Greetings from Austria"; ?> 

What happens when script.php is executed?

 Hello World Greetings from Austria 

Both variables will be displayed. All variables included in vars.php are visible in script.php as well.

Static variables, like many other components of PHP, have been borrowed from C/C++. Static variables can be used to build functions that are able to remember values. Especially for recursions, this is an important feature. To show you what you can do with static variables, we have included a brief example:

 <?php         for     ($i=1; $i<=6; $i++)         {                 echo display_data($i)."<br>\n";         } function display_data($i) {         static $var;         $var .= "$i";         return $var; } ?> 

A loop makes sure that display_data is called six times. $var is defined as a static variable, which means that it will remember the value it had the last time display_data was called. Every time the function is called, the current value of $i is added to $var.

If you execute the script, you will see that the length of $var grows every time display_data is processed:

 1 12 123 1234 12345 123456 

Static variables have disadvantages as well as advantages. Bugs caused by static variables are sometimes difficult to find, and it can be very painful to look for errors that can only be found by extensive debugging.



PHP and PostgreSQL. Advanced Web Programming2002
PHP and PostgreSQL. Advanced Web Programming2002
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 201

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net