PHP Include files
In PHP, include and require are both used for including external files in a PHP script. These external files can contain functions, variables, HTML, or other PHP code that you want to reuse across multiple pages. The primary difference between them lies in how they handle errors and whether the script execution continues when the included file is not found:
1. include Statement:
include("file_to_include.php");
require Statement:
require("file_to_require.php");
Use include when you want to include a file that may not be critical to the operation of your script, and you want the script to continue running even if the included file is missing or encounters an error.
Use require when you want to include a file that is essential for the operation of your script, and you want to ensure that the script doesn't continue if the included file is missing or has errors.
Both include and require statements are followed by a file path that points to the external file you want to include. When you use them, the code from the included file becomes part of the script, and you can access functions, variables, and code defined in that external file.
For example :
<?php include("header.php"); ?>
<?php require("config.php"); ?>
In next chapter we will practice file handling
Share this page on :
© 2022 AnalyzeCode.com All rights reserved.