Using external files
You can save development time by creating separate pages for particular elements and then incorporating them into the main PHP pages using specific functions. Two of these functions are include() and require():
include ('file.php'); require ('file.html');
Both functions work the same way, with one relatively insignificant difference: If an include() function fails, the PHP script generates a warning but continues to run. Conversely, if require() fails, it terminates the execution of the script.
But what do these two functions do? Both include() and require() incorporate the referenced file into the main file (for clarity’s sake, the file that has the include() or require() line is the including or parent file). The result is the same as if the included code were part of the parent file in the first place.
Understanding that basic idea—including a file makes it as if that file’s contents were in the parent script to begin with—is key to making the most out of this feature. This means that any code within the included file not within PHP tags is treated as HTML. And this is true regardless of what extension the included file has (because it’s the extension of the including file that counts).
There are many reasons to use included files. You could put your own defined functions into a common file. You might also want to place your database access information into a configuration file.
Both include() and require() have variations: include_once() and require_once(). Each is identical to its counterpart except that it ensures that the same file can be included only one time (in a parent script). You
should generally avoid using these, as they’ll adversely affect the script’s performance.