Help - Search - Members - Calendar
Full Version: PHP and "<<<"
Invision Power Services > Community Forums > Community Web Design and Coding
Trel
I was looking at an online tutorial and they had code like this

CODE
function someFunction() {
return <<<HTML

<b>Some HTML</b>

HTML;
}


Can someone explain the <<< thing to me and how that syntax works?
IAIHMB
It's the beginning of the HEREDOC syntax.
James Mathias
IPB uses HEREDOC in it's skin functions, except IPB uses <<<EOF instead of 'HTML', I think you can use anything you like so long as it's not a reserved word, and you close the statement with the same word, ie. EOF;

Another thing to keep in mind is that the close word cannot have any whitespace around it as it'll fail with a parse error.

An advantage to using this method is that you don't have to escape double quotes as is the case with double quoted strings like print and echo.

A disadvantage (in my opinion) is it lowers the readability of the code, as you cannot indent properly, but the trade off is having harder to read html output with escaped(\) characters.
Brendon Koz
QUOTE(James Mathias @ Apr 17 2006, 10:16 AM) *
IPB uses HEREDOC in it's skin functions, except IPB uses <<<EOF instead of 'HTML', I think you can use anything you like so long as it's not a reserved word, and you close the statement with the same word, ie. EOF;

Eww, it does? I guess that's the only bad thing about skinning/templating. There really isn't any "elegant" way to do it in PHP.
James Mathias
Yup, as seen here;

CODE
//===========================================================================
// <ips:bbcode_wrap_end:desc:>
//===========================================================================
function bbcode_wrap_end() {
$IPBHTML = "";
//--starthtml--//


$IPBHTML .= <<<EOF
</div><p>
EOF;

//--endhtml--//
return $IPBHTML;
}


Of course you'll never see it like this unless you prefer working with the flat cache files, as opposed to the acp interface, which I do.
Grant
I thought that you couldn't edit the flat cache files considering that the cache doesn't update the database. So how exactly do you edit those without the ACP
.Logan
Was wondering the same, as if you do edit the flat cache files once you update the cache they are overwritten with what is in the database.

I'm assuming he wrote a tool or some sort of plugin to sync the database by the cache files instead of how it is now, the database syncing the cache files.

Regardless, it's not a good idea to do it that way.
Michael P
I think you just set "IN_DEV" in the ACP, and it does a rebuild of the skins from the files to the database? I may be wrong, it just rings a bell
James Mathias
I edit the cache files directly, then when I need to, I set IN_DEV to 1 in init.php then go to the skin tools and rebuild the database from cacheID

This allows me to work on the files in a text editor, and they don't get overwritten so long as I don't rebuild the caches, which I never do except upon exporting, in which case I use the little trick I described to get sync the files.

I hope that helps someone, I just dislike the built in editor.
Brendon Koz
I'm so tempted to purchase a license just so I can tinker around the code. I could care less about the script's purpose. biggrin.gif
ZuCruTrooper2
QUOTE(malikyte @ Apr 17 2006, 08:10 PM) *
I'm so tempted to purchase a license just so I can tinker around the code. I could care less about the script's purpose. biggrin.gif


Its a great way to enhance your knowledge of the language...

I've learned a lot from the way IPB is laid out, the way the code is organized, coding methods, and the use of comments, and have applied the methods to my own work. It is definitely some nice code to work with if your goal is to build up knowledge.
Luke
I used to use EOF for my template system too... But I switched to normal variables because you arnt able to escape variables... Like have something like this $var1[$var2]. The thing is you have to worry about quotes if you do that, whereas with EOF it really doesnt matter what you put inside.
=Charles
QUOTE(Cy @ Apr 20 2006, 01:20 AM) *
I used to use EOF for my template system too... But I switched to normal variables because you arnt able to escape variables... Like have something like this $var1[$var2]. The thing is you have to worry about quotes if you do that, whereas with EOF it really doesnt matter what you put inside.

You could always put curly brackets around it.

{$var1[$var2]}
Starnox
What do curly brackets do again?
IAIHMB
It's the "complex" string parser. For example:

CODE
<?php
    $tiffany[0][1] = "Hooray for boobies!";
    echo "She said: $tiffany[0][1]";
?>


Will give you "She said: Array[1]", and:

CODE
<?php
    $tiffany[0][1] = "Hooray for boobies!";
    echo "She said: {$tiffany[0][1]}";
?>


Will give you "She said: Hooray for boobies!".

Edit: I suppose that wasn't very thorough. Perhaps the following from mister Rasmus will make more sense:

QUOTE
This method can be used either to disambiguate or to interpolate array lookups. The classic use of curly braces is to separate the variable name from surrounding text.


Without the curly braces, PHP doesn't know that it should look for first index in the second dimension.
Starnox
Ah right cheers original.gif
Luke
Brackets are used for inserting variables into strings. They arnt required for normal variables, but required for arrays. {$var1[$var2]} doenst work the last time I checked. Not in EOF. That's why I created {# ... #} in my template system to escape it from a string (after I changed mine to use strings).

So:

This is my output {$var} and this is too {#$var[$var2]#}, yay! {#$var[$var2]#}

Results in:

$OUTPUT .= "This is my output {$var} and this is too".$var[$var2].", yay!".$var[$var2];

Of cource with this you have a security issue, but my CMS is still in alpha. I'll get around to it tongue.gif
Trel
[face_cry] I REALLY need to stop hoping for simple answers xD
UBERHOST.NET
QUOTE(Trel @ Apr 20 2006, 09:43 PM) *
[face_cry] I REALLY need to stop hoping for simple answers xD

Delimiting strings with quotes forces you to use escape characters when you want to use quotes in the string. Defining your own delimiters with HEREDOC allows you to use quotes inside of your string without having to use escape characters.
.Kennedy
QUOTE(John Henry @ Apr 20 2006, 11:14 AM) *
Its a great way to enhance your knowledge of the language...

I've learned a lot from the way IPB is laid out, the way the code is organized, coding methods, and the use of comments, and have applied the methods to my own work. It is definitely some nice code to work with if your goal is to build up knowledge.


Yeah, it really helps ae. I've learnt so much from it, just reading the code.
Trel
QUOTE(kewlceo @ Apr 21 2006, 01:27 AM) *
Delimiting strings with quotes forces you to use escape characters when you want to use quotes in the string. Defining your own delimiters with HEREDOC allows you to use quotes inside of your string without having to use escape characters.


Thanks. I think I understood that far, but when using { and } around some variables or arrays or whatever, that's where I got lost.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.