Website design require header,footer,side bar,right bar, ad bar on multiple pages.It is not possible to change these on thousand of web pages whenever we want to change anything. Include function in php solve this problem. We can use include function in PHP and include the content of a PHP file into another PHP file before the server executes it.So If there is any change required then instead of changing thousand of files just change included file.
PHP has two functions which can be used to included one PHP file into another PHP file.
The include() Function
The require() Function
The include() Function
The require() Function
Both the function has different working .
The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement
Include function in Php | Require function in php |
The include() function takes all the text in a specified file and copies it into the file that uses the include function.
Syntax
Include ‘<file_name>’
Example
<?php
include ‘/home/test/public_html/coded/fbscript.php’;
?>
| The require() function takes all the text in a specified file and copies it into the file that uses the require function.
Syntax
require ‘<file_name>’
Example
<?php
Require ‘/home/test/public_html/coded/fbscript.php’;
?>
|
If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.
If in above example if the fbscripts.php does not exists,then still code will run and it will just generate warning
| If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.
If in above example if the fbscripts.php does not exists,then still code will halt
|
Useful examples for Include function in Php
<!doctype html>
<html class=”no-js” lang=”en”>
<head>
<meta charset=”utf-8″ />
<title>Example Site</title>
<?php include ‘/home/app//public_html/comcode/stylesheets_depth.php’; ?>
<?php include ‘/home/app//public_html/comcode/javascript.php’; ?>
</head>
<body>
<?php include_once(“/home/app//public_html/comcode/analytics.php”) ?>
<?php include ‘/home/app//public_html/comcode/brow-update.php’; ?>
<?php include ‘/home/app//public_html/comcode/facebooscript.php’; ?>
<?php include ‘/home/app//public_html/comcode/header1.php’; ?>
< text here>
</body>
<?php include ‘/home/app//public_html/comcode/footer.php’; ?>
</html
|
The above page will still even if the php script are not present as we have used include
<?php
require_once “/home/app/public_html/header1.php”;
?>
<!doctype html>
<html class=”no-js” lang=”en”>
<head>
<TITLE> Example site </TITLE>
<META NAME=”revisit-after” CONTENT=”1″>
<?php include ‘/home/app/public_html/comcode/stylesheets.php’; ?>
<?php include ‘/home/app/public_html/comcode/javascript.php’; ?>
</head>
<body>
<?php include_once(“/home/app/public_html/comcode/analytics.php”) ?>
<?php include ‘/home/app/public_html/comcode/brow-update.php’; ?>
<?php include ‘/home/app/public_html/comcode/facebookscript.php’; ?>
<?php include ‘/home/app/public_html/comcode/header2.php’; ?>
<text here>
</body>
<?php include ‘/home/app/public_html/comcode/footer.php’; ?>
</html>
|
The above will fail if the first header file is not available as it uses the require function
0 comments:
Post a Comment