...

View Full Version : Can a for loop be put in a heredoc?



milesdriven
07-25-2012, 08:20 PM
I'd like to thank everyone for helping me. I've studied the replies carefully. I'm much closer to figuring out how to do this. I thought about posting the code when I have it working, but its probably too long.

I know a heredoc can be put in a loop, because I've done it. I also know brackets are needed for variables and arrays placed in a heredoc, like this:


print <<<HOW
{$_SESSION['user_id']}
{$variable}
HOW;


Can a for loop go into a heredoc as well, if it is placed in brackets?

An example:


{// Opening bracket
for($k = 1; $k <= 6; $k++)
{
echo $k;
}
}// Closing bracket

Fou-Lu
07-25-2012, 09:15 PM
Heredoc is a string handling replacement. You can only put into it what you can put into a string.
Loops, assignments, calculations, any expression or call cannot be placed in a heredoc.

Braces are not required for heredoc. They are required for ambiguous variables and complex types.


BTW, these reasons are a part of why I consider heredoc and nowdoc to be much lesser in usefulness over a standard string.

DrDOS
07-25-2012, 09:16 PM
It's this simple to do and you haven't tried it? I haven't used heredocs yet, but they look kind of useful, so I might try it myself.

tangoforce
07-26-2012, 12:40 AM
I also know brackets are needed for variables and arrays placed in a heredoc, like this:


print <<<HOW
{$_SESSION['user_id']}
{$variable}
HOW;


Thats not correct. You do not have to do that, as Fou says, you can do it if you want to (EG for multidimensional arrays its useful) but you don't have to. This is the normal way - Note the missing ' marks for $_SESSION:



print <<<HOW
$_SESSION[user_id]
$variable
HOW;




Can a for loop go into a heredoc as well, if it is placed in brackets?

An example:


{// Opening bracket
for($k = 1; $k <= 6; $k++)
{
echo $k;
}
}// Closing bracket


No, as Fou says, the heredoc is purely for assembling strings in a quick and easy fashion with minimal fuss over multiple lines. It is not an operator and will not actually execute PHP code.

Dormilich
07-26-2012, 08:29 AM
@Fou-Lu: but they are quite useful if you need a text with both ' and " (saves a lot of escaping).

tangoforce
07-26-2012, 11:35 AM
BTW, these reasons are a part of why I consider heredoc and nowdoc to be much lesser in usefulness over a standard string.


The nowdoc is actually very useful when you want to transmit PHP code to another server via post to be run on that server via eval (for quick things such as wiping logs once a week etc) :D

Fou-Lu
07-26-2012, 03:26 PM
Escaping is a non-issue really. Its like needing to add a semi-colon after an instruction. I'd still rather take my ability to compile expressions and function calls to a string than to extract them to variables and use in heredoc. Not to mention that ending delimiter rule has always pissed me of.
But, at least its not a goto.

Dormilich
07-26-2012, 03:28 PM
Not to mention that ending delimiter rule has always pissed me of.

absolutely.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum