116Part IPHP: The BasicsIncluding only (Web host forum) onceSometimes you really
116Part IPHP: The BasicsIncluding only onceSometimes you really want a file to be included once, but not more than once. This is truemost often in the case of function definitions. For example, two different function definitionfiles might, in turn, include the same file of utility functions if a top-level page includes bothof these files, the utility functions might be included twice, leading to complaints from PHPthat functions are being defined twice. To the rescue come include_onceand require_once, which act just like their counterpartsexcept that they will not include a file named by a given string if that file has already beenincluded. It s usually better to use the _onceversion, in general, for including function andclass definition files. The include pathWhen you includea filename, PHP searches for a file by that name in the directories speci- fied in the include_path(which is settable in your php.inifile). The default path includesthe same directory as the one the top-level code page is in. See Chapter 30 for details abouthow to add locations to your includepath. In situations where a single instance of PHP serves several virtual sites, it s generally easierand less confusing to PHP to use the $_SERVERsuperglobal array to specify the location of anincludefile: include_once($_SERVER[ DOCUMENT_ROOT ]. /path/to/include_file ); Remember that included (and required) files are parsed by default in HTML mode ratherthan in PHP mode. This means that any included file meant to be interpreted as PHP needsto have the usual PHP tags at the beginning and end. RecursionSome compiled languages, like C and C++, impose somewhat complex ordering constraints onhow functions are defined. To know how to compile a function, the compiler must know aboutall the functions that the function calls, which means the called functions must be definedfirst. So what do you do if two functions each call the other or if one function calls itself? Issues like this led the designers of C to a separation of function declarations (or prototypes) from function definitions (or implementations). The idea is that you use declarations to informthe compiler in advance about the types of arguments and return types of the functions youplan to use, which is enough information for the compiler to handle the actual definitions inany order. In PHP, this problem goes away, and so there is no need for separate function prototypes. Aslong as each function that is called is defined once (and only once) in the current code file orone that is included in the course of the current script s execution, PHP will have no problemresolving function calls, regardless of the interleaving of function calls and definitions. This means that recursive functions(functions that call themselves) are no problem in PHP4. For example, we can define a recursive function and then immediately call it: function countdown ($num_arg) { if ($num_arg > 0) { print( Counting down from $num_arg
); Caution08
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.