Skip to content
PHP Logo

How can PHP be used to generate HTML content without using the echo command?

To generate HTML content without using the echo command in PHP, you can use output buffering. Output buffering allows you to capture the output generated by PHP and store it in a buffer before sending it to the browser. This can be useful when you want to generate HTML content dynamically without mixing it with PHP code.

php
<?php
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Output Buffering Example</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>
<?php
$html_content = ob_get_clean();
echo $html_content;
?>