Heredoc

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
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Heredoc

Post by Straterra »

Can you use a while statement in a heredoc statement?
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've tried that and had no luck doing so, if theres a soultion i'll be happy
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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 &lt;= 10)
&#123;
        echo 0++ . "
";
&#125;
End While Statment

End Heredoc
Sorry, you'll have to end the heredoc, do the while statement and then start another heredoc.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

You could add the strings to a var (or vars) then echo them at your leisure.

Another option is output buffering.
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post 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.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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;
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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. :)
Post Reply