Prev | Current Page 151 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


?– Note This section applies only to parameters passed by value and not to those passed by reference.
Parameters passed by reference will indeed be affected by any changes made to the parameter from
within the function. If you don??™t know what this means, don??™t worry about it because Chapter 4 addresses the
topic in some detail.
Function parameters are declared after the function name and inside parentheses.
They are declared much like a typical variable would be:
78 CHAPTER 3 ?–  PHP B ASICS
// multiply a value by 10 and return it to the caller
function x10 ($value) {
$value = $value * 10;
return $value;
}
Keep in mind that although you can access and manipulate any function parameter in
the function in which it is declared, it is destroyed when the function execution ends.
You??™ll learn more about functions in Chapter 4.
Global Variables
In contrast to local variables, a global variable can be accessed in any part of the program.
To modify a global variable, however, it must be explicitly declared to be global in the
function in which it is to be modified. This is accomplished, conveniently enough, by
placing the keyword GLOBAL in front of the variable that should be recognized as global.
Placing this keyword in front of an already existing variable tells PHP to use the variable
having that name. Consider an example:
$somevar = 15;
function addit() {
GLOBAL $somevar;
$somevar++;
echo "Somevar is $somevar";
}
addit();
The displayed value of $somevar would be 16.


Pages:
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163