PHP Include File Function
PHP Include is used to embed a PHP File into another File. Mainly it is used to add the library of user-defined functions.
Example 1 – Include File Code to Embed Scripts
<?php function fact($x) // creating a function { $fact1=1; for($i=1; $i<=$x; $i++) { $fact1 = $fact1 * $i; } return $fact1; } ?>
<!DOCTYPE html> <html> <head> </head> <body> <?php include('factorial.php'); echo fact(5) . " is the factorial of 5" . "<br />"; echo fact(6) . " is the factorial of 6" . "<br />"; echo fact(7) . " is the factorial of 7" . "<br />"; echo fact(13) . " is the factorial of 13" . "<br />"; ?> </body> </html>
In the above example, we have created 2 PHP files and one PHP file will be called into another PHP file.
Note:You have to save both the PHP files in the same folder.
PHP Require File Function
require Function is also similar to the include function. It is also used to embed a PHP file to another PHP file.
Example 2 – Require File Code
<?php function fact($x) // creating a function { $fact1=1; for($i=1; $i<=$x; $i++) { $fact1 = $fact1 * $i; } return $fact1; } ?>
<!DOCTYPE html> <html> <head> </head> <body> <?php require('factorial.php'); echo fact(5) . " is the factorial of 5" . "<br />"; echo fact(6) . " is the factorial of 6" . "<br />"; echo fact(7) . " is the factorial of 7" . "<br />"; echo fact(13) . " is the factorial of 13" . "<br />"; ?> </body> </html>
In the above example again, we have created 2 PHP files and one PHP file will be called into another with using the require function.
PHP include vs require Function Difference
When you embed a PHP script file with include function and file not found, the server will continue the execution without that file. Means browser receives the response without included file.
When you embed a PHP script file with require function and file not found, the server will stop the execution without that file and raise error. Means browser does not receives the response without required file.
The below screenshot shows the fatal error generated by the server when require function is used and the called file does not exist.
PHP include_once
include_once function embed a file but avoid embedding a file multiple times by mistake. Many times included file have function definitions and multiple time including a file generate error (multiple function definition).
When you embed a PHP script file with include_once function and file not found, the server will continue the execution without that file. Means browser receives the response without file.
PHP require_once
When you embed a PHP script file with require_once function and file not found, the server will stop the execution without that file and raise error. Means browser does not receives the response without required file. It also guard to call a file multiple times as compared to .
Hi there! I could have sworn I’ve been to this website before but after checking through some of the post I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be bookmarking and checking back often!