Store embedded HTML inside a variable in PHP

I was interested, mainly because I think I saw it somewhere earlier, if you can store HTML inside a variable, something like the following (I know this does not make sense, just to clarify my question) :

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

And then $var will be equal to text goes here

+8
variables html php
source share
3 answers

You can do this using output buffering. Take a look at the examples in ob_get_contents () and ob_start () .

 <? ob_start(); ?> All kinds of stuff, maybe some <?= "php"; ?> etc. <? $var = ob_get_contents(); ?> 
+16
source share

Perhaps you are thinking of the Heredoc syntax :

 <?php $var = <<<EOD text goes here EOD; ?> 
+6
source share

Discard this facebook blog post on XHP, a language that allows XML literals in PHP code.

0
source share

All Articles