Print entire php page

Pretty simple question, I believe. How to print the entire page in a variable and then use it where I need it.

For example, if the code:

<?php

$arr = array('hello','mate','world');
foreach ($arr as $a) {print "<p>".$a."</p>"; }

?>

Now, if we go to this page, we will see the output of the array, but I would prefer to print the entire page in a variable and then generate a static page from it.

Perhaps, file_get_content or <<<EOTbut the page will be more complex later, so I'm not sure what is the best option.

+3
source share
3 answers

Not sure about your specific needs, but:

ob_start();
require('/path/to/templates/foo.php');
$template = ob_get_contents();
ob_get_clean();
+8
source
ob_start();

// your code

$var = ob_get_clean();

print $var;
+1
source

Why don't you use smarty ,

put all the HTML in the template and paste the PHP code or variables into it. at the end, using $x=$smarty->fetch('template_name');, you put the whole page in the variable $ x ...

+1
source

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


All Articles