How to PHP Scripting- Include & Require Functions
The include & Require statements include and evaluate the specified file. It is generally good practice to use the include function when writing code, and then later as you review it to replace it with the require function. Based on their overall purpose, they do exactly the same thing. They both “include” a file such as another PHP file that includes a standard header for your website.
the difference between the two functions is that the require function stops executing your code as soon as an error occurs. The include function would still continue executing.
<?php include 'header.php'; ?> <div id="content"> </div> <?php include 'footer.php'; ?>
PHP Include function
The include() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.
PHP Require function
The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require function halt the execution of the script and generate an error.
|include() function takes one argument , a path/name of the specified file you want to include.||require() function performs the similar functionality as include() function, takes one argument i.e. path/name of the file you want to include.|
|include() function does not stop execution , just give warning and continues to execute the script in case of file name not found i..e missing/misnamed files .||require() function gives fatal error if it founds any problem like file not found or misnamed files and stops the execution once get fatal error.|
|include() function can be used in loops or control structure.||require() function can not be used in loops or control structures.|
|You can return a value from an included file|
Ex: $result = include rtnvalue.php;
|File included as the result of a require statement cannot return a value.|
|You can use include when file is not required or not so important .||When file is required by the script, better to use require() instead of include().|
PHP include_once Function
The include_once function is exactly the same as the include function except it will limit the file to be used once.
include_once('name of the called file with path');
PHP require_once Function
PHP require_once Function is a combination of the require and include_once function. It will make sure that the file exists before adding it to the page if it’s not there it will throw a fatal error. Plus it will make sure that the file can only be used once on the page. This function is the most strict out of the four functions and is the function I use most when constructing the page
require_once('name of the called file with path');