I've created a menu on the upper part of the page (in PHP). Now I would like all pages to contain it but I don't want to use frames and i prefer not to use <<< / EOF to edit the menu content.
Is there any other way I can do this?
I've created a menu on the upper part of the page (in PHP). Now I would like all pages to contain it but I don't want to use frames and i prefer not to use <<< / EOF to edit the menu content.
Is there any other way I can do this?
Use PHP include()
:
<?php
include('menu.php');
//rest of your code goes here
The other alternatives are:
require()
require_once()
include_once()
The require() function is identical to include()
, except that it handles errors differently. If an error occurs, the include()
function generates a warning, but the script will continue execution. The require()
generates a fatal error, and the script will stop.
The require_once
statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.
If in doubt, read this answer.
The include_once
statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again.
If you're confused about which one to use, check out these answers:
Hope this helps!
Use an include
(or require
):
... stuff before the menu
<?php include 'menu.php'; ?>
... rest of my page here
Since the question asks for "any other way to do this" and since php include and require has already been proposed, here is another way using javascript (jquery):
<html>
<head>
... (meta tags etc.)
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script>
<script type='text/javascript'>
$(function() {
$('#menu').load('menu.php');
});
</script>
</head>
<body>
<div id='menu'></div>
... (page content etc)
</body>
</html>