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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post

    Putting variables in a heredoc, when the heredoc is in a for loop?

    To pose my question in a way that makes it clear what I'm asking, I'll start with a simple example. Here is a heredoc:
    Code:
    $tree = <<<DEMO
    This is day 1
    DEMO;
              print $tree;
    I want to place this heredoc in a for loop, so it is repeated once for each day of the month. I'll use January as an example. I want to place variables in the heredoc shown above, so the iteration for each day of the month is unique so php will create 31 of them.

    My thought is the $tree variable and the DEMO delimiter both need information about the days in January incorporated into them to make each iteration truly unique.

    The example below is what I think I need to do. I put the raw data in instead of the variables to try to make the question I'm asking clear. The example below would work to make one iteration that is Jan 1 2012:
    Code:
    $treeJan12012 = <<<DEMO1
    DEMO1;
                      print $treeJan12012;

    This example would be Jan 2 2012:
    Code:
    $treeJan22012 = <<<DEMO2
    DEMO2;
                      print $treeJan22012;
    What I'm trying to do is something like this:
    Code:
    $month = date('M');
    $days = date('t');
    $year =('Y')
    
    for($k = 1, $k <= $days, $k++)
    {
    $tree.$month.$days.$year = <<<DEMO$days
    DEMO$days;
                      print $tree.$month.$days.$year;
    }
    What I just did in the example above doesn't look right, but it shows you what I'm trying to do. How do I put these variables in there so php will use them to make 31 unique heredocs?

    Thank you
    Last edited by milesdriven; 07-21-2012 at 04:50 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I don't understand what you are trying to do here. Whilst you can certainly make use of a variable variable if you wanted to (written as: ${$tree . $month . $days . $year} = 'a value';), I don't see how this will help you at all, nor the need for the use of heredoc at all.

    Using variable variables is rarely required and adds a complication since you don't have an explicitly defined variable name. Use arrays instead which are far more valuable.

    So what exactly is it you are trying to do here, and how are you going to use the data after? Are you trying to build a calendar?

  • #3
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Yes, I'm trying to build a calendar. If you remember the thread where I was trying to use concats to echo html and javascript to the browser line by line. The heredoc worked well.

    Since it worked so well, I want to put it in the for loop that creates the calendar. I need to place the variables the for loop creates in the heredoc somehow, so the loop reproduces it one time for each day of the month.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I'm still trying to get over the need for the variable variables. Why are you needing to store these anywhere?

    If you are immediately printing within the loop, then you can simply print the heredoc instead of containing it in a variable, exactly as if it were a string. You can also call that variable anything you want and print it before the end of the loop and it will simply overwrite the original variable that was there if you wanted to use the variable. But I see no need to create a unique variable for it, but if you do need to store it it would be better to use arrays instead. Given a calendar, simple dimensions of year and month would be sufficient and the resulting array is the days (or keys are the days if the data is something in particular).

    Heredoc itself cannot be anything but a constant value. This is invalid: <<< CONSTNAME$var. This must be a constant identifier, just like label. This is quite irrelevant though, as heredoc is translated and stored upon request, not as a heredoc statement, so the name doesn't matter (ever).

  • #5
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    In other words, I can do this:
    Code:
    for($k = 1, $k <= 31, $k++)
    {
              <<<DEMO;
    Today is $k;
    DEMO;
              print DEMO;
    }
    and the for loop will print this :
    Code:
    Today is 1
    Today is 2
    etc..
    Is this right?

    I thought I needed to put variables everywhere because I thought everything that was going to be iterated had to contain them somehow. You just showed me the heredoc delimiter can't contain them, because it has to be a constant - and the heredoc itself does not need to be contained within a variable. That means the variable $tree is not necessary, and complicates things.

    Which further tells me as long as the variables between the heredoc delimiters are unique per iteration, all will work well.

    Oops, php didn't like this part:
    Code:
    print DEMO;
    Got this error:
    Code:
    Use of undefined constant 'DEMO'
    Last edited by milesdriven; 07-21-2012 at 05:48 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, DEMO isn't a constant, so you cannot print it like that.
    Just print it directly:
    PHP Code:
    $iDaysInMonth cal_days_in_month(CAL_GREGORIAN22012);
    for(
    $k 1$k <= $iDaysInMonth$k++)
    {
        print <<<DEMO
    Today is $k;
    DEMO;

    You can use cal_days_in_month to get the number of days in a month and year provided.

  • #7
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    So, the only place variables are required for my purposes is in the heredoc itself.

    I did this:
    Code:
    for($k = 1, $k <= 31, $k++)
    {
            print <<<DEMO
    Today is $k;
    DEMO;
    }
    It works.
    Thanks very much once again for your help.
    Last edited by milesdriven; 07-21-2012 at 06:05 PM.


  •  

    Posting Permissions

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