Page 1 of 1

saving content in a variable (cfsavecontent)

Posted: Sun Dec 14, 2003 6:50 pm
by returnButton
ok, this might be simple; i need to save a big block of HTML in a variable, thus:

instead of doing:
$string="";
$string.="some html";
$string.="some html";
$string.="some html";
echo $string;

something like:
$string=
{
lots of HTML
lots of HTML
lots of HTML
}
echo $string;

i need this so i can do other things with the $string before i output it, and i'd rather stick the HTML down on the page than have to build it up in a string manually.

in coldfusion there's a tag called cfsavecontent, which is my friend. is there anything similar in PHP?

thanx..

Posted: Sun Dec 14, 2003 7:10 pm
by Weirdan

Posted: Sun Dec 14, 2003 7:28 pm
by infolock
Weirdan wrote:http://php.net/manual/en/language.types ... ax.heredoc
[php_man]ob_[/php_man]* functions.
just giving an example...

Code: Select all

$string= <<<foo
lots of HTML 
lots of HTML 
lots of HTML 
foo;

//......somewhere down the road...over the rainbow...whatever

echo $string;

Posted: Sun Dec 14, 2003 9:19 pm
by dull1554
so your gonna just call the string in a echo like this?

Code: Select all

<?php
$string = "loads of html";

if (blah == blah)
{
echo $string;
}

else
{
echo "die die die";
}
?>
as long as you dont have to use the contents of the string anywhere else you could just do this

Code: Select all

<?php
if (blah == blah)
{
Print <<< EOT
loads of html goes here
EOT;
}

else
{
echo "die die die";
}
?>
and if you have to use the $string somewhere else what infolock said is perfectly fine too.
but in my opinion less code == better

Posted: Mon Dec 15, 2003 3:21 am
by Nay
Image

Grrr.........too late.

Urm.....nevermind me.

-Nay

Posted: Tue Dec 16, 2003 2:22 pm
by returnButton
thanks folks; so do y'all use this "Heredoc" a lot?

it's a weird function, but i tried it and it does the trick : )

Posted: Tue Dec 16, 2003 2:30 pm
by DuFF
Nay seems to like it alot :D

But I'm not really too into it. I usually just echo it in a variable or just exit out of the PHP and code it straight into the HTML.

Example:

Code: Select all

<?php
$variable = " blah ";
$variable = trim($variable);
?>
<table>
  <tr><td>
<?php
echo $variable;
?>
  </td></tr>
</table>

Posted: Tue Dec 16, 2003 3:31 pm
by dull1554
I myself love Heredoc and use it ALWAYS, and duff yes you can exit out of the php command, but as far as i'm concerned thats pointless if your gonna go right back into it, i also find that Heredoc instead of echo or print can make a script a bit easier to comprehend......best of luck with heredoc, and i hope you can figure out everything you wish to acomplish