Page 1 of 1
Heredoc
Posted: Fri Jan 16, 2004 5:30 pm
by Straterra
Can you use a while statement in a heredoc statement?
Posted: Fri Jan 16, 2004 5:48 pm
by dull1554
i've tried that and had no luck doing so, if theres a soultion i'll be happy
Posted: Fri Jan 16, 2004 5:49 pm
by DuFF
test.php:
Code: Select all
<pre>
<?php
$i = 0;
echo <<<EOD
Start Heredoc
Start While Statement:
while ($i <= 10)
{
echo $i++ . "<br>";
}
End While Statment
End Heredoc
EOD;
?>
</pre>
Output:
Code: Select all
Start Heredoc
Start While Statement:
while (0 <= 10)
{
echo 0++ . "
";
}
End While Statment
End Heredoc
Sorry, you'll have to end the heredoc, do the while statement and then start another heredoc.
Posted: Sat Jan 17, 2004 7:44 am
by Nay
<_< *The heredoc god has landed*
Heredoc is meant to output everything that's after it before it sees it's end anchor. How did you expect it to function?
Also you can't do this:
Code: Select all
echo <<< NOO
this is a htmlentities($text) html text!
NOO;
-Nay
Posted: Sat Jan 17, 2004 9:48 am
by Straterra
Well, the whole point of me using Heredoc is returning multiple lines from a function. The reason I need this is because I am getting information from a database, and it outputs at 3 different times. The first is the HTML tag that tells the browser where to send the information after the user has clicked Submit. The second is the contents of the actual drop-down menu. The third ends the dropdown manu and provided the user with the Submit and Reset buttons.
Posted: Sat Jan 17, 2004 3:17 pm
by McGruff
You could add the strings to a var (or vars) then echo them at your leisure.
Another option is output buffering.
Posted: Sat Jan 17, 2004 4:43 pm
by Straterra
I can't really use a heredoc..With a while statement, it is easier to just do it the way I originally did it.
Posted: Sat Jan 17, 2004 9:11 pm
by m3rajk
you could do this:
$string1='';
$string2='';
$string3='';
while(something){
/* make string one that has all the info needed for that print out */
}
while(something){
/* make string two that has all the info needed for that print out */
}
while(something){
/* make string three that has all the info needed for that print out */
}
echo <<<end
<!-- html and the strings -->
end;
Posted: Sat Jan 17, 2004 9:17 pm
by Gen-ik
Nay wrote:Also you can't do this:
Code: Select all
echo <<< NOO
this is a htmlentities($text) html text!
NOO;
True. But you could do this...
Code: Select all
<?php
$text = htmlentities($text);
echo <<< WHOOHOO
This is my text... {$text} ...good init?
WHOOHOO;
?>
HEREDOC is good for somethings but not for others. It's better to use it only when you
need to and not because you
can.
