Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store inline HTML within variable in PHP

I was wondering, mostly because I think I've seen it before somewhere, if it is possible to store HTML within a variable, something like the following (I know this makes no sense, it's just to clarify my question):

<? $var = ' ?>
text goes here
<? '; ?>

And then $var would equal text goes here

like image 777
Kokos Avatar asked Jun 09 '11 13:06

Kokos


2 Answers

You could do that using output buffering. Have a look at the examples at ob_get_contents() and ob_start().

<? ob_start(); ?>

All kinds of stuff, maybe some <?= "php"; ?> etc.

<? $var = ob_get_contents(); ?>
like image 189
Niklas Avatar answered Oct 23 '22 22:10

Niklas


You may be thinking of the Heredoc syntax:

<?php
$var = <<<EOD
text goes here
EOD;
?>
like image 28
jensgram Avatar answered Oct 23 '22 23:10

jensgram