115Chapter 6Control and FunctionsFunction ScopeAlthough the rules about (Web hosting directory)
115Chapter 6Control and FunctionsFunction ScopeAlthough the rules about the scope of variable names are fairly simple, the scoping rules forfunction names are even simpler. There is just one rule in PHP5: Functions must be definedonce (and only once) somewhere in the script that uses them. (See the following note aboutdifferences between this behavior and PHP3.) The scope of function names is implicitlyglobal, so a function defined in a script is available everywhere in that script. For clarity ssake, however, it is often a good idea to define all your functions first before any code thatcalls those functions. In PHP3, functions could be used only after they were defined. This meant that the safestpractice was to define (or include the definitions of) all functions early in a given script, before actually using any of them. PHP4 and 5 actually precompile scripts before runningthem, and one effect of this precompilation is that it discovers all function definitions beforeactually running the code. This means that functions and code can appear in any order in ascript, as long as all functions are defined once (and only once). Include and requireIt s very common to want to use the same set of functions across a set of Web site pages, andthe usual way to handle this is with either includeor require, both of which import thecontents of some other file into the file being executed. Using either one of these forms isvastly preferable to cloningyour function definitions (that is, repeating them at the beginningof each page that uses them); when you want to modify your functions, you will have to do itonly once. (We covered these forms in Chapter 4, but they are worth reviewing here in thecontext of including function definitions.) For example, at the top of a PHP code file we might have lines like: include basic-functions.inc include advanced-function.inc ; (.. code that uses basic and advanced functions ..) which import two different files of function definitions. (Note that parentheses are optionalwith both include()and require().) As long as the only things in these files are functiondefinitions, the order of their inclusion does not matter. Both includeand requirehave the effect of splicing in the contents of their file into thePHP code at the point that they are called. The only difference between them is how they failif the file cannot be found. The includeconstruct will cause a warning to be printed, but processing of the script will continue; require, on the other hand, will cause a fatal error ifthe file cannot be found. Note that includeand requireare now more similar in their behavior than they used tobe. Prior to PHP 4.0.2, requirehad its file contents spliced in statically, before the actualexecution of the page; whereas the contents from includewere spliced in dynamically as the page executed. Among other things, this led to subtle differences in behavior whenthe include/requireform was in conditional code. Now, however, both includeandrequirehave the same dynamic behavior. This means, for example, that if aninclude/requireform is in a loop executed 10 times, 10 inclusions will be made. NoteNote08
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.