Search code examples
phpwhitespacedouble-quoteslinefeed

How does source code manage white space and concatenation


In the following code:

    $email_body = 
       "message from $name \n" 
       "email address $visitor_email \n"
       "\n $message";

the fourth line generates a parsing error dues to an unexpected ", but the quotes seem to be correctly paired. So why is (the final?) one in the last line "unexpected"?

I expected the result for $email_body to be:

    message from $name
    email address $visitor_email 

    $message

I've looked throught the syntax page on php.net/manual, and read the questions here on single and double quotes. I can't find any exceptions for a line feed at the beginning of a string but that seems to be what it is. Can anyone clarify?


Solution

  • Don't break the string up like that unless you use . to concatenate them back together!

    So, either:

     $email_body = 
       "message from $name \n 
       email address $visitor_email \n
       \n $message"; // also the whole string on one line is fine
    

    or

    $email_body = 
       "message from $name \n" 
       . "email address $visitor_email \n"
       . "\n $message";