August 17, 2024
External PHP file inclusion
You can include separate PHP files in your current file. Through including separate PHP files in your current file you can reuse your code again and again with writing only once. You can standardize your code with including separating files. You can write separate PHP files for different purpose according to your need and include in your other files wherever you it. There are two functions 'include()' and 'require()' present in PHP, which you can use for external file inclusion in current PHP file. These two function are almost same and takes only one parameter which is file name you want to include in your current file. You can write separate PHP files for performing specific tasks.
PHP include() function
PHP include() function: include() function is used for PHP file inclusion in current file. It takes only one parameter which is file name you want to include in your current file. The include() function generates a warning if the file you are including does not exist. But the main benefit of include is that it generates a warning on error and script will continue execution. It does not terminate script when the file you are including does not exist. The example of include() function is mentioned below:
Code
<html>
<body>
<?php include("FirstPHPFile.php"); ?>
<h1>File Inclusion Example</h1>
</body>
</html>
In the above mentioned example we included a 'FirstPHPFile.php' in the current script. Suppose if due to some reason 'FirstPHPFile.php' does not exist in the current directory then on your webpage a warning error will appear but your script will not be terminated, it will remain continue.
PHP include_once() function
PHP include_once() function: include_once() function quite similar to include() function. It takes only one parameter which is file name you want to include in your current file. The only difference between include_once() and include() function is that suppose you have already included a file in your script and again you are trying to include it, then the file will be included again in your current file and an error message will be there. But if you are trying to include a file more than one time with using include_once() then this function will not let you include the file again more then once even if you are trying to include. It is the main benefit of include_once() function. The example of include_once() function is mentioned below:
Code
<?php
echo "Example of include_once";
include_once("FirstPHPFile.php");
?>
PHP require() function
PHP require() function: require() function is used for separate PHP file inclusion in current file. It takes only one parameter which is file name you want to include in your current file. The require() function generates a fatal error if the file you are including does not exist. The main difference between require() and include() is that it generates a fatal error and stop execution your script. It terminates script execution when file does not exist. The example of require() function is mentioned below:
Code
<html>
<body>
<?php require("FirstPHPFile.php"); ?>
<h1>File Inclusion Example</h1>
</body>
</html>
In the above mentioned example we included a 'FirstPHPFile.php' in the current script. Suppose if due to some reason 'FirstPHPFile.php' does not exist in the current directory then on your webpage a fatal error will appear and your script will be terminated, it will be stop your script or program where error will occur.
PHP require_once() function
PHP require_once() function: require_once() function quite similar to require() function. It takes only one parameter which is file name you want to include in your current file. The only difference between require_once() and require() function is that suppose you have already included a file in your script and again you are trying to include it, then the file will be included again in your current file and an error message will be there. But if you are trying to include a file more than one time with using require_once() then this function will not let you include the file again more then once even if you are trying to include. It is the main benefit of require_once() function. The example is mentioned below:
Code
<?php
echo "Example of require_once";
require_once("FirstPHPFile.php");
?>