Results 1 to 8 of 8

Thread: Proper php usage for .= <<<END... END;

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default Proper php usage for .= <<<END... END;

    Does anyone know the proper usage of this php structure? I have Googled it and cannot find anything... not sure what it is called.

    Code:
    $variable .= <<<END
    contents
    END;
    It seems as if it may be a more elegant way to assign multi-line values to a variable but I get an error message when I try it. Not sure of the correct format for the contents. Can you include variables? Do you enclose text in quotes? No clue. Thanks.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    http://php.net/manual/en/language.types.string.php
    Look for the section on "Heredoc syntax".

    That's a weird structure that allows you to avoid escaping. If you are using something that contains many ' and " then it might make sense. But realistically I don't see the point in using it because it is so odd.

    I don't see any typos in your code, but maybe it isn't capable of performing the .= operation. Try just = instead to see if that works. It also might not be installed/enabled in your PHP configuration. I don't know anything about this specific issue, but hopefully this will help you start narrowing it down.

    The effect is that <<<XYZ .......... XYZ; is basically the same as "............" but just fancier.

    Note that you can use new lines in a string easily. Just use a new line inside the quotes just like you'd add any other character. If you would prefer to keep your code on one line, you can use \n as a line break (or \r\n or just \r\ depending on the OS) within double quotes (it's literally \n in single quotes).



    EDIT: Take a look at "Nowdoc" on the same page. That might be more useful for your purposes because it looks like the Heredoc syntax is limited in some ways.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (02-09-2011)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    The only practical use I've ever had for heredocs is when I'm using php to dynamically generate javascript code (where I'm likely to use both single and double quotes, and don't want to keep escaping them). But I try to avoid it- weirder than it's worth.

  5. The Following User Says Thank You to traq For This Useful Post:

    kuau (02-09-2011)

  6. #4
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    This is it! Thanks so much. So if I read it correctly, I don't need to use quotes or dots to concatenate text and variables? So would this be correct?

    Code:
    $body = <<<END
    Name:      $_POST['name']\n; 
    Email:      $_POST['email']\n; 
    Phone:     $_POST['phone']\n; 
    END;
    It's to replace this:
    Code:
    $body1 = "Name:      ".$_POST['name']."\n"; 
    $body1.= "Email:      ".$_POST['email']."\n"; 
    $body1.= "Phone:     ".$_POST['phone']."\n";
    $body1.= "Agent:      ".$_POST['agent']."\n";
    That's just a few lines of the code. I'm simply trying to find the best (simplest, cleanest, most efficient) way to create the body of an email.

    Mahalo for your help!

  7. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    You could also do

    PHP Code:
    $body1 "Name:   {$_POST['name']}  \n"
    $body1.= "Email:  {$_POST['email']} \n"
    $body1.= "Phone:  {$_POST['phone']} \n";
    $body1.= "Agent:  {$_POST['agent']} \n"
    Corrections to my coding/thoughts welcome.

  8. The Following User Says Thank You to bluewalrus For This Useful Post:

    kuau (02-09-2011)

  9. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    PHP Code:
    $body = <<<END
    Name:      $_POST['name']\n; 
    Email:      
    $_POST['email']\n; 
    Phone:     
    $_POST['phone']\n; 
    END; 
    unfortunately, that gets you this:
    Code:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING [...]
    Complex variables (read: indexes) still mess things up. Bluewalrus' {brackets suggestion} actually fixes it:
    PHP Code:
    $body = <<<END
    Name:      {$_POST['name']}\n; 
    Email:      
    {$_POST['email']}\n; 
    Phone:     
    {$_POST['phone']}\n; 
    END; 
    As a side note - and your ending sequence was correct, kauu, but I can see this being overlooked - you will also get a parse error if your END; is not followed immediately by a line break. So, this is correct:
    PHP Code:
    END;
    // there's another line right here 
    but this is not:
    PHP Code:
    END//this is the last line in the script (also, there's whitespace and a comment after the END; ) 
    Last edited by traq; 02-09-2011 at 03:42 PM.

  10. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    I'm not familiar with using curly brackets except in css. What do they mean here? Thanks.

  11. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    They're similar to the brackets in an if statement.

    An if statement takes a single statement. For example:
    if ($x==1) echo '1';

    In order to make it take more than that one statement, you use brackets to surround them and make them act like one:
    ... { echo '1'; echo '2'; }
    The brackets contain 2 statements, but they are read together (with the brackets) as 1 statement.

    Now after that tangent, this particular instance is a bit of an exception but it follows basically the same idea. It tells PHP to group the characters between the brackets as a single unit.

    In double quotes (or heredoc structures) you can include variables:
    echo "$foo";

    But you can't use an array index in exactly that way because it tries to parse it as literal text and gets confused. So you help PHP by telling it that you are using the array and index as a unit:
    echo "{$foo['bar']}";

    Of course in that example it seems like overkill, but in something like what you've posted or a longer string it makes sense to have a way to include variables within the string.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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