In this tutorial you will learn how to include and evaluate the files in PHP.
Including a PHP File into Another PHP FileThe include() and require() statement allow you to include the code contained in a PHP file within another PHP file. Including a file produces the same result as copying the script from the file specified and pasted into the location where it is called.
You can se a lot of time and work through including files — Just store a block of code in a separate file and include it wherever you want using the include() and require() statements instead of typing the entire block of code multiple times. A typical example is including the header, footer and menu file within all the pages of a website.
The basic syntax of the include() and require() statement can be given with:
include("path/to/filename"); -Or- include "path/to/filename"; require("path/to/filename"); -Or- require "path/to/filename";Tip: Like the print and echo statements, you can omit the parentheses while using the include and require statements as demonstrated above.
The following example will show you how to include the common header, footer and menu codes which are stored in separate 'header.php', 'footer.php' and 'menu.php' files respectively, within all the pages of your website. Using this technique you can update all pages of the website at once by making the changes to just one file, this ses a lot of repetitive work.
ExampleRun this code » Tutorial Republic Welcome to Our Website!Here you will find lots of useful information.
Difference Between include and require StatementsYou might be thinking if we can include files using the include() statement then why we need require(). Typically the require() statement operates like include().
The only difference is — the include() statement will only generate a PHP warning but allow script execution to continue if the file to be included can't be found, whereas the require() statement will generate a fatal error and stops the script execution.
ExampleRun this code » Welcome to Our Website!Here you will find lots of useful information.
Tip: It is recommended to use the require() statement if you're including the library files or files containing the functions and configuration variables that are essential for running your application, such as database configuration file.
The include_once and require_once StatementsIf you accidentally include the same file (typically functions or classes files) more than one time within your code using the include or require statements, it may cause conflicts. To prevent this situation, PHP provides include_once and require_once statements. These statements behe in the same way as include and require statements with one exception.
The include_once and require_once statements will only include the file once even if asked to include it a second time i.e. if the specified file has already been included in a previous statement, the file is not included again. To better understand how it works, let's check out an example. Suppose we've a 'my_functions.php' file with the following code:
ExampleRun this code »Here's is the PHP script within which we've included the 'my_functions.php' file.
ExampleRun this code »