Once you have created a script or an HTML content that you want to use in multiple pages is best stored in an external file and included in each
page you want to use it. This is accomplished with the include and require functions.
PHP has four commands that can be used to include code from an external file:
include('external_file.php');OR
require('external_file.php');
<?php include('C:/xampp/htdocs/includes/file.php'); // windows include('/usr/MAMP/htdocs/includes/file.php'); // unix (linux) ?>
<?php include('file.php'); // "file.php" is in the curent script directory include('../includes/file.php'); // "file.php" is in the "includes" folder, which is in the back directory ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Title page</title> <body> <?php include('templ/header.php'); ?> <div id="menu"> <?php include('templ/menu.php'); ?> </div> <div id="content">some content</div> </body> </html>With this method you can insert the same header and menu code in any PHP files in the Web site, all pages in the Web site can include the code of the header and menu files, and if you need to update the header or the menu later, you will only need to make changes in the "header.php" or "menu.php" file.
Using parentheses with the include commands is optional, the following would also work:
include 'includes/file.php';
include '/usr/MAMP/htdocs/includes/file.php';
When using a relative file path, you can use use "./" to indicate that the path begins in the current folder. Example:
include('./includes/file.php');
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250"> <param name="src" value="file.swf" /> Your browser not support SWF. </object>
input:focus { background-color: #88fe88; }
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}'; var obj = JSON.parse(jsnstr); alert(obj.url);
if (file_put_contents("file.txt", "content")) echo "The file was created"; else echo "The file can not be created";