Quick Reach
The require function introduction
The require function almost works similar to PHP include except, the way an error is handled. If there is some problem in loading the included file by using the require function e.g. given path is incorrect or accidentally misspelled the file name then require function will generate a Fatal Error. Whereas the same problem in include function will generate just a warning error message.
The code of file name referred in the calling PHP file will be copied there before execution in PHP require function.
Syntax of require function
require(“file_name.php”);
Example of using require
1
2
3
4
5
6
7
8
9
10
11
|
<?php
require(“header.php”);
require(“left_nav.php”);
//All main body code goes here
require(“footer.php”);
?>
|
The require file path
If you have an include folder, for example, then the above example would look like this
1
2
3
4
5
6
7
8
9
10
11
|
<?php
require(“include/header.php”);
require(“include/left_nav.php”);
//All main body code goes here
require(“include/footer.php”);
?>
|
So left_nav.php and footer.php files are placed inside the include folder (for above example) while calling PHP file is beside or outside of the include folder.
Leave A Comment?