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!

1
  • George Cumming's answer is best. Regards Commented Jun 25, 2013 at 16:37

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

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.

1 Comment

function file_get_contents is great solution cuz I can put it in other function so it gives me good level of incapsulation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.