Include text from other files in PHP

I want to create templates for my website.

My template

So let's say this is mine template_header.php:

<html>
    <head>
        <title>
            '.$email_subject.'
        </title>
    </head>
    <body>

And this is mine template_footer.php:

    </body>
</html>

Problem

As I know, I can use output buffering to include these patterns. This is my code:

<?php
ob_start();
include "template_header.php";
?>
<p>Text</p>
<?php
include "template_footer.php";
$message = ob_get_contents();
ob_end_clean();
?>

But that will not work! $messageis empty! Even when I do var_export($message), I get nothing !!! Even an empty string.

  • What is the problem with my code?
  • What other ways can I include my text?
+4
source share
1 answer

This works for me.

ob_start(); 
//print all the things - and I mean print. echoing and plain html work.
$message = ob_get_clean();

echo $message;
0
source

Source: https://habr.com/ru/post/1685393/


All Articles