HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [224]
Tons of naming schemes are out there, and even if you don’t use either of these, picking one and being consistent is important. Searching for naming variables in Google returns more than one million hits, so plenty of resources are available.
So, if you have a variable that you want to access and modify from within the function, you either need to pass it through the parentheses or access it with the global modifier.
The following code will print hello world! only once:
$output = “
hello world!
”;function helloWorld(){
global $output;
print $output;
}
function helloWorld2(){
print $output;
}
helloWorld();
helloWorld2();
?>
I left the global keyword off in the helloWorld2() function, so it didn’t print at all because inside the function, the local variable $output is undefined. By putting the global keyword on in the helloWorld() function, I let it know I was referring to a global variable defined outside the function.
PHP defaults to local inside functions because it doesn’t want you to accidentally access or overwrite other variables throughout the program. For more information about global and local scoping, check out http://us3.php.net/global.
Returning data from functions
At the end of the function, you can tell the function to return one (and only one) thing. The return statement should be the last statement of your function. The return statement isn’t required, but it can be handy.
The getName() function in the following code example will return world to be used by the program. The program will print it once and store the text in a variable to be printed multiple times later, as shown in the following code and Figure 5-2:
PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
function getName(){
return ”World”;
}
print ”
Hello, ” . getName() . ”!
”;$name = getName();
print << $name, welcome to our site. We are so very happy to have you here. If you would like to contact us, $name, just use the form on the contact page. HERE; ?> Figure 5-2: An example of a function with a return statement. The example in Figure 5-2 is admittedly contrived. This function could easily be replaced by a variable, but the program that uses the function doesn’t know that the function has only one line. Later on, I could make the function much more complicated (maybe pulling the name from a database or session variable). This points out a very important feature of functions that return values: they can feel like variables when you use them. Server-side programming is very handy, but it has one major flaw. Every connection to the server is an entirely different transaction. Sometimes, you’ll want to reuse a variable between several calls of the program. As an example, take a look at rollDice3.php in Figure 5-3. Figure 5-3: This page displays a roll, the number of rolls, and the total rolls so far. The interesting feature of rollDice3.php happens when you reload the page. Take a look at Figure 5-4. This is still rollDice3.php, after I refreshed the browser a few times. Take a look at the total. It increases with each roll. The rollDice3.php program is interesting because it defies normal server-side programming behavior. In a normal PHP program, every time you refresh the browser, the program starts over from scratch. Every variable starts out new. Figure 5-4: The count and total values keep on growing. Understanding session variables The rollDice3.php program acts differently. It has a mechanism for keeping track of the
Managing Persistence with Session Variables