saving content in a variable (cfsavecontent)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
returnButton
Forum Newbie
Posts: 6
Joined: Sat Jul 05, 2003 5:06 pm

saving content in a variable (cfsavecontent)

Post 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..
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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;
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Image

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

Urm.....nevermind me.

-Nay
returnButton
Forum Newbie
Posts: 6
Joined: Sat Jul 05, 2003 5:06 pm

Post 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 : )
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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>
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
Post Reply