PHP echo command that parses a variable

larry98765

Registered
Hi All,

I'm putting blocks of HTML code into variables using herdoc format ($var=<<<EOF etc.) and I'd like those blocks to sometimes include parse-able php, so that when the variable is later called, php should execute any php it finds.

But as I expected, when I put in a php chunk into the HTML, the variable doesn't get properly set, because I'm nesting php within php in a sense. Here's what I mean:

<?php

$foo= <<<EOF

<h4>Some HTML code</h4>
<p>Some more html code</p>
<?php SOME PHP CODE ?>
<p>Some more html code</p>
EOF;

?>

and later on

<?php echo $foo ?>

PHP doesn't seem to like this, and I don't blame it. So what's the solution?
 
The command you are looking for is eval. Eval allows you to execute a string as if it were php.

To get your example to work, you will need to modify it to be valid php for the eval to parse. This means putting quotes around your strings and ending lines with semicolons.

Here is your example modified to work:

<?php
$foo= <<<EOF
"<h4>Some HTML code</h4>
<p>Some more html code</p>";
PHP Code Here, you don't need the pis (<? & ?>) and always end with a semicolon ;
"<p>Some more html code</p>";
EOF

$bar = eval($foo);
echo $bar;
?>
 
Back
Top