-3

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?

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
SagiLow
  • 5,721
  • 9
  • 60
  • 115

3 Answers3

4

Use PHP include():

<?php
include('menu.php');

//rest of your code goes here

The other alternatives are:

  • require()
  • require_once()
  • include_once()

require()

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.

require_once()

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.

include_once()

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:

#2418473 #3626235 #11051219

Hope this helps!

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

Use an include (or require):

... stuff before the menu

<?php include 'menu.php'; ?>

... rest of my page here
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
0

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>
Stefan
  • 3,850
  • 2
  • 26
  • 39