1

I wonder how can I insert an html document into another one using php. E.g.:

header.html

<div id="header">Hey this is header</div>

index.php

<body>
<? get_document('header.html'); ?>
...

I could use ajax for that but I dont want my web site to depend on js. Thank you!

Ivan
  • 437
  • 1
  • 7
  • 17

3 Answers3

1

You can use include() to include one document in another:

<body>
<?php include('header.html'); ?>
...

If you want execution to fail if the file is not present, use require():

<body>
<?php require('header.html'); ?>
...

Be aware that short-open tags (<?) do not work on all servers and are disabled by default in new version of PHP. For this reason, it is best to use the full-open tag: <?php

Community
  • 1
  • 1
George Cummins
  • 28,485
  • 8
  • 71
  • 90
1
<html>
...
<body>
<?php
      include("header.html");
?>
</body>
</html>

Or if you want to store it in variable and use later as you wish

   ob_start();
   include("header.html");
   $content = ob_get_contents();
   ob_end_clean();


   echo $content;
Dimash
  • 581
  • 1
  • 5
  • 13
0

You can just use include/require. That can be done on any file, not just PHP files.

You could also do echo file_get_contents('header.html'); but include makes more sense in this case.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • function file_get_contents is great solution cuz I can put it in other function so it gives me good level of incapsulation. – Ivan Jun 25 '13 at 16:48