-6

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?

talvi
  • 39
  • 10

3 Answers3

1

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";
delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
0

When I run it, the error is on the third line, not the fourth.

The problem here is that you have a string literal next to another string literal with no operator between them.

This is wrong:

"foo" "foo" 

as is:

"foo"
"foo"

You could do something like this:

"foo" . "foo"

or

"foo" . 
"foo"
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Your error is

Error on line 5: parse error, unexpected T_CONSTANT_ENCAPSED_STRING("email address $visitor_email \n"

This is where your error starts

$email_body = 
       "message from $name \n" // This line you need to add concatination
       "email address $visitor_email \n"

Then your code will look like

 <?php

 $email_body = 
       "message from $name \n". 
       "email address $visitor_email \n".
       "\n $message";
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34