In PHP there is a feature which allows to write code in a different file and call it where ever there is a need for executing the code. this can be done by using the include and require functions.
with the help of the these two functions we can perform the same :
- The include() Function
- The require() Function
these two function allows to call the file to execute. But the main difference between both is that
The Include function does the loading of the code of the file and if there is any error in loading then the include will generate the error message and displays it but continue execution.
On the other hand the require function loads the file but if any error occur then the require function will displays the fatal error and halts the execution of the code.
The example of the include function as :
<html>
<body>
<?php include("menu.php"); ?>
<p>This is an example to include PHP file!</p>
</body>
</html>
In the above code if there is any error even though the execution continues and displays the error message.
while in require it is not the case.
<html>
<body>
<?php require("menu.php"); ?>
<p>This is an example wrong PHP file!</p>
</body>
</html>
In the above code if the required function is used to call the file and any error occurred then it halts the execution and doesn't proceed.
It is always recommended to use require() instead of include() function, as the execution should halt when any error occurred.
0 Comment(s)