<?php ... ob_start(); ?> <html> <head>...</head> <body>...<?php echo $another_variable ?></body> </html> <?php ... $variable = ob_get_clean(); ?>