Changes

PHP Functions

65 bytes added, 20:45, 31 May 2007
Functions and Variable Scope
== Functions and Variable Scope ==
Now that we have covered PHP functions it is time to address an issue relating to variable scope. When a variable is declared outside of a function it is said to have ''global'' scope. That is, it is accessible to any part of teh PHp the PHP script in which it is declared. Conversely, when a variable is declared inside a function it is said to have ''local'' scope, in that it is only accessible to the script contained in the body of the function.
This means that you can have a global variable and a local variable with the same name containing different values. In the following example the $myString variable is declared in both local and global scope with different string values:
</pre>
Clearly this will be a problem if you ever need to access a global variable in a function with a conflicting local variable name. Fortynately Fortunately PHP provides the ''GLOBALS'' array which provides access to all global variables from within functions. If, therefore, we wanted to access the global version of $myString from within the ''showString()'' function, which contains a local version of $myString, we could modify our script as follows:
<pre>