Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is \n not working on one line of my PHP script, but is fine on all the others?

I'm writing an e-mail message to be sent on submission of a form. For some reason, the \n character displays correctly on all lines but one. I can't see any difference between this line and the others, so can't work out why it isn't working. The code is below, together with the output that I'm getting as well as a list of the variables used.

As you can see from the email, the \n on the end of the 'Work Address' line is not working, so "Company Name" just follows straight on, rather than being on the next line. Then the \n after company name works fine, so home address is on the next line.

Here's the code for the message:

$xmsg = "First Name: $firstname\n" .
        "Last Name: $lastname\n" .
    "Work Tel: $worktel\n" .
    "Home Tel: $hometel\n" .
    "Mobile Tel: $mobtel\n" .
    "E-mail: $email\n" .
    "Work Address: $workad\n" .
    "Company Name: $company\n" .
    "Home Address: $homead\n" .

Here's the output:

First Name: Joe
Last Name: Bloggs
Work Tel: 0123456789
Home Tel: 0987654321
Mobile Tel: 0789456123
E-mail: [email protected]
Work Address: Bloggoffice, Joe Street, London, N1 J0E Company Name: 
Home Address: 9 Blogg Street, Borough of Joe, London, SE4 J03

And in case it's useful, here's the list of variables used:

$firstname = $_SESSION['firstname'];
$lastname = $_SESSION['lastname'];
$worktel = $_SESSION['worktel'];
$hometel = $_SESSION['privtel'];
$mobtel = $_SESSION['mobtel'];
$email = $_SESSION['email'];
$company = $_SESSION['companyname'];
$workad = $_SESSION['workad1'] . ', ' . $_SESSION['workad2'] . ', ' .     $_SESSION['workad3'] . ', ' . $_SESSION['workpostcode'];
$homead = $_SESSION['homead1'] . ', ' . $_SESSION['homead2'] . ', ' . $_SESSION['homead3'] . ', ' . $_SESSION['homepostcode'];

I'd be grateful for any help, I hope it's not just a stupid mistake that I've overlooked!

like image 523
Chris Avatar asked Nov 13 '22 11:11

Chris


1 Answers

Using PHP_EOL constant seems more correct than using \n. Because PHP_EOL is the correct end of line character for every platform.

like image 73
cnkt Avatar answered Nov 16 '22 04:11

cnkt