Page 1 of 1

<<< and Eggs

Posted: Wed Oct 15, 2003 12:38 pm
by Gen-ik
I've recently discovered the use of <<< in PHP and like a dog that's just found a burried bone I've been going <<< crazy.

There are a couple of things I'm not sure about though and can't find anything in ye'old manual about it.


In this example how would I get the variable $EGG to display properly bearing in mind that I need some text directly after it (no spaces).

Code: Select all

$EGG = "Boil";
echo <<<EGGS
My egg is now $EGGed
EGGS;
..and I know I could just set $EGG as "Boiled" but it's just an example.

Also... is it possible to run commands inside of <<< END?
Example...

Code: Select all

$EGGS = 2;
echo <<<moreEGGS
I now have ($EGGS*2) eggs
moreEGGS;
// needs to display... "I now have 4 eggs"
I've tried loads of ways of getting both of these minor niggles ironed out but nothing seems to work yet.


Any help would be great as usual :)

Posted: Wed Oct 15, 2003 12:51 pm
by volka
You can use the complex (curly) syntax to mark the begin and end of an variable identifier

Code: Select all

<?php
$char = 'b';
echo <<< EOD
a{$char}c
EOD;
?>
afaik you can only perform table lookups (scalars, arrays, object properties) but not function calls.

Posted: Wed Oct 15, 2003 1:14 pm
by Gen-ik
So easy...... and it's the one thing I didn't think about trying even though I use curlys in echo()s and stuff! Doh. :roll:

Posted: Wed Oct 15, 2003 6:27 pm
by Bill H
Can somebody tell me what <<< is, that is point me to documentation for it?

I cannot find it in the downloaded doc I have, nor in php.net online documentation.
That is a search for it comes up with no result, and it does not appear in any list of operators that I can find.

Posted: Wed Oct 15, 2003 6:28 pm
by JAM

Posted: Thu Oct 16, 2003 3:08 am
by Nay
mwhahahah, more and more people are getting into heredoc :lol:

-Nay

Posted: Thu Oct 16, 2003 9:02 am
by Gen-ik
Well, I know why now... it makes getting big chunks of (dymanic) HTML onto the page a hell of a lot easier.

It's a shame most people are finding Heredoc by accident though, there should be an obvious reference or link to it near the echo() and print() parts of the manual.

Posted: Thu Oct 16, 2003 9:15 am
by m3rajk
from http://se2.php.net/manual/en/function.echo.php
echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
END;
the orielly book covers it when it covers "" and '' but doesn't mention what it is. i didn't realize how useful it was till someone else pointed it out. i'd do <?php to start file?>some html<? $variable ?>more html....

someone also said it works faster because it's easier for the parser