<<< and Eggs

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

<<< and Eggs

Post 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 :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

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

Post 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:
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

mwhahahah, more and more people are getting into heredoc :lol:

-Nay
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

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

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