tags:

views:

115

answers:

2

Mixing html and php is simple:

<? while($row): ?>
    <p><?=$row['name'] ?></p>
<?php endwhile; ?>

But how could i store html like this into a php variable? (Purposed syntax, but doesn't work)

<? $html = ?>
    <p>My HTML!!</p>
<? ; ?>
+4  A: 

This is how:

$html = <<< HTML
    <p>My HTML!!</p>
HTML;

That is know as php heredoc

Note: You should place closing heredoc keyword without any spaces or indentation.

Sarfraz
thanks, the note was kinda important :)
tarnfeld
@tamfeld: yes it was :)
Sarfraz
A: 

You could use the heredoc syntax when declaring the string:

<?php
    $html = <<<HTML
    <p>My HTML!!</p>
HTML;
?>

This treats anything between the start <<<HTML (HTML is the identifier) and the end HTML as a string. Note that the closing identifier must be the first in a new line without any indention.

Gumbo
<< won't work !!!!!
Sarfraz
I use `<<<` and not `<<`!
Gumbo
@Gumbo: you initially used only <<. I am not saying that i have more knowledge than you probably but you must have mis-typed it. thanks :)
Sarfraz
@Gumbo: and moderators do have this stored, i mean edits. thanks
Sarfraz
So why the down vote?
Gumbo
as i said << doesn't work, +1 for you correcting it. thanks
Sarfraz