Jump to content

PHP concatenation


Recommended Posts

I found this code on php.net page


// The message
$message = "Line 1\nLine 2\nLine 3";

// Send
mail('caffeinated@example.com', 'My Subject', $message);

but when I test with an echo statement like this:-

$message = "Line 1\nLine 2\nLine 3";
echo "$message";

in WampServer2 there aren't any line breaks. Why Not? Has php.net given a wrong example? How can I get \n or \r\n to create a line break from a variable?


I really want to concatenate several variables like:-

$name = "Name";
$email = "Email";
$message = "Message";
$messagebody = $name . "\r\n" . $email . "\r\n" . $message;
echo "$messagebody";

but there aren't any line breaks, it's all on one line. How do I make \r\n work?

Link to comment
Share on other sites

I am intending to use \r\n in a concatenated variable in an email function but I thought I would test it first. Why should it work in an email function but not in an echo statement?


Would it make any difference if it was a plain text email or if I included these headers

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

to make an html email?

Note that it's used in the headers code, but of course that doesn't print out.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...