Variable Creation and Scope PHP

Although you've seen variables in the previous pages, you may wonder what they are exactly. Part of a computer is called RAM, or random access memory. This is a volatile medium for storing information. That is, it all disappears when you shut off the machine. The computer sees this memory as a long string of single characters, or bytes, each numbered. In PHP, however, you cannot actually get to memory at this level. You must use a variable. You provide a name, and PHP takes care of matching the name to physical memory.

Experimenting with Scope

Experimenting with Scope

You do not need to let PHP know about a variable before you use it. Some languages like C require you to declare every variable along with its type. This is because a specific amount of memory needs to be set aside. But this is generally a problem associated with ompiled languages, not interpreted ones. The first time you use a variable in PHP, the engine adds it to the list of variables it knows about and makes a best guess at what type of data the variable holds.

The first place you use a variable establishes the scope—the range within the code in which the variable may be seen. Every function you define has its own variable space. That is, there are variables that exist just for that function, and they are invisible to all other parts of your script. In addition there is a global scope for variables created outside any function. In some programming languages global variables are visible inside functions. This is not the case with PHP. When you create a function in PHP, you must explicitly tell PHP you want a global variable to be present in the function. Listing uses the metaphor of the United States to demonstrate.

The script sets up a function, printCity, that prints out the name of a city. It will be used to show the contents of the variables named capital. Variables is plural because there are actually three variables in the script named capital. One is global and the other two are local to the California and Utah functions.

When you run this script you will find that the cities are printed in the order Washington DC, Sacramento, Salt Lake City, and Washington DC. Notice that even though we have given capital a new value inside California, it is not the same variable we set to Washington DC. The variables inside California and Utah exist within their own space and are created and destroyed each time the functions are called.

It is important to remember that when you create a variable inside a function, it exists only while that function is executing. Once execution finishes and control is passed back the calling process, all the variable space for that function is cleaned up. Sometimes this is not desirable; sometimes you want the function to remember the values of the variables between calls. You could implement this by using global variables, but a more elegant solution is to use the static command.

At the beginning of a function, before any other commands, you may declare a static variable. The variable will then retain any value it holds, even after leaving the function. You might wonder why you would ever need to do this. Suppose you'd like to build a table where the rows alternate in background color. Listing does just this.
Demonstrations of Static Variables

Demonstrations of Static Variables

Demonstrations of Static Variables

Listing will print out a table with 10 rows. Each row will alternate background colorsbetween an intense green and a lighter green. I have used this technique in a project where I pulled data from a database and separated rows with alternating blue and green lines. Instead of using background colors, I chose between single-pixel images that I stretched to span the browser window.

Take another look at Listing. The printCity function takes an argument called NameOfCity. When the function is called, the variable is set with the value passed in the function call. In all other respects the variable is the same as ther local variables.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics