Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post

    Can a for loop be put in a heredoc?

    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:
    PHP Code:
         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:
    PHP Code:
    {// Opening bracket
         
    for($k 1$k <= 6$k++)
         {
                 echo 
    $k;
         }
    }
    // Closing bracket 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

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

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,903
    Thanks
    15
    Thanked 226 Times in 226 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Quote Originally Posted by milesdriven View Post
    I also know brackets are needed for variables and arrays placed in a heredoc, like this:
    PHP Code:
         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:

    PHP Code:
         print  <<<HOW
    $_SESSION[user_id]
    $variable
    HOW; 
    Quote Originally Posted by milesdriven View Post
    Can a for loop go into a heredoc as well, if it is placed in brackets?

    An example:
    PHP Code:
    {// 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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    @Fou-Lu: but they are quite useful if you need a text with both ' and " (saves a lot of escaping).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Quote Originally Posted by Fou-Lu View Post
    Edit:
    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)
    Last edited by tangoforce; 07-26-2012 at 10:47 AM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    Quote Originally Posted by Fou-Lu View Post
    Not to mention that ending delimiter rule has always pissed me of.
    absolutely.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •