Judul : PHP include() and require() FUNCTIONS
link : PHP include() and require() FUNCTIONS
PHP include() and require() FUNCTIONS
PHP include and require Statements :
It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.
The include and require statements are identical, except upon failure:
- require will produce a fatal error (E_COMPILE_ERROR) and stop the script
- include will only produce a warning (E_WARNING) and the script will continue
The include() Function :
The include() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.
Assume you want to create a common menu for your website. Then create a file menu.php with the following content.
<a href="http://www.phpdevelopmenttricks.blogspot.com/index.htm">Home</a> -
<a href="http://www.phpdevelopmenttricks.blogspot.com/ebxml">ebXML</a> -
<a href="http://www.phpdevelopmenttricks.blogspot.com/ajax">AJAX</a> -
<a href="http://www.phpdevelopmenttricks.blogspot.com/perl">PERL</a> <br />
Now create as many pages as you like and include this file to create header. For example now your test.php file can have following content.
<html>
<body>
<?php include("menu.php"); ?>
<p>This is an example to show how to include PHP file!</p>
</body>
</html>
The require() Function :
The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.
So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.
You can try using above example with require() function and it will generate same result. But if you will try following two examples where file does not exist then you will get different results.
<html>
<body>
<?php include("xxmenu.php"); ?>
<p>This is an example to show how to include wrong PHP file!</p>
</body>
</html>
This will produce the following result −
This is an example to show how to include wrong PHP file!
Now lets try same example with require() function.
<html>
<body>
<?php require("xxmenu.php"); ?>
<p>This is an example to show how to include wrong PHP file!</p>
</body>
</html>
This time file execution halts and nothing is displayed.
Difference between include & include_once or require & require_once:
The difference include vs. include_once and require vs. require_once is obvious from the names.
include
and require
allow you to insert a file multiple times within a single execution lifetime. On the other hand,include_once
and require_once
make sure one file is inserted only once in a single execution lifetime, even if your CODE calls them multiple times.Difference between include & require or include_once & require_once:
Another significant difference is: include vs. require or include_once vs. require_once. Even though naming of these constructs are almost perfect according to their functionality, many PHP programmers fail to understand the significant difference.
As the name suggests, with
include
or include_once
, you simply include other files in the flow of execution. On the other hand, when you use require
or require_once
, you not only include the file in the flow of execution, but also you tell PHP that the execution cannot be continued without that file.Demikianlah Artikel PHP include() and require() FUNCTIONS
Sekianlah artikel PHP include() and require() FUNCTIONS kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
Anda sekarang membaca artikel PHP include() and require() FUNCTIONS dengan alamat link https://othereffect.blogspot.com/2016/06/php-include-and-require-functions.html
0 Response to "PHP include() and require() FUNCTIONS"
Post a Comment