Web hosting domains - 111Chapter 6Control and FunctionsAs of PHP 4.1, there
111Chapter 6Control and FunctionsAs of PHP 4.1, there is a small set of global variables that areautomatically visible fromwithin function definitions, in contradiction to the previous paragraph and the following one. These are the superglobal arrays($_POST, $_GET, $_SESSION, and so on), which containkeys and values corresponding to variable bindings from different sources. For more onthese variables and their uses, see Chapter 7. The only variable values that a function has access to are the formal parameter variables(which have the values copied from the actual parameters), plus any variables assignedinside the function. This means that you can use local variables inside a function withoutworrying about their effects on the outside world. For example, consider this function and its subsequent use: function SayMyABCs () { $count = 0; while ($count < 10) { print(chr(ord( A ) + $count)); $count = $count + 1; } print(
Now I know $count letters
); } $count = 0; SayMyABCs(); $count = $count + 1; print( Now I ve made $count function call(s).
); SayMyABCs(); $count = $count + 1; print( Now I ve made $count function call(s).
); The intent of SayMyABCs()is to print a sequence of letters. (The functions chr()and ord() translate between letters and their numeric ASCII codes we use them here just as a trick togenerate letters in sequence.) The output of this code is: ABCDEFGHIJNow I know 10 lettersNow I ve made 1 function call(s). ABCDEFGHIJNow I know 10 lettersNow I ve made 2 function call(s). Both the function definition and the code outside the function make use of variables called$count, but they refer to different variables and do not clash. The default behavior of variables assigned inside functions is that they do not interact withthe outside world; they act as though they are newly created each time the function is called. Both of these behaviors, however, can be overridden with special declarations. Global versus localThe scope of a variable defined inside a function is localby default, meaning that (as weexplained in the previous section) it has no connection with the meaning of any variables out- side the function. Using the globaldeclaration, you can inform PHP that you want a variableNote08
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.