-2
<?php
echo 'Hello';
echo 'World':

$e = // Get the output of the page.

echo $e; // 'HelloWorld' should be printed.
?>

I want to get all the output of the current page and print it as a variable.

For example, I printed 'Hello' and 'World' respectively. How can I get the content on the page?

Vindows 95
  • 11
  • 2

1 Answers1

1

Use Output Buffering:

ob_start();
echo 'Hello';
echo 'World':

$e = ob_get_clean();

echo $e; // 'HelloWorld' should be printed.

More information can be found in the documentation: https://www.php.net/manual/en/ref.outcontrol.php

maiorano84
  • 11,574
  • 3
  • 35
  • 48
  • Worth noting that this assumes that output buffering is not already enabled, or that the buffer is empty beforehand. YMMV depending on buffering settings and application state. – Sammitch Mar 03 '23 at 21:31