Alert boxes in JavaScript provide a useful way to show the user an error, message or prompt. Adding new lines to one of these alert boxes can be done like so:
alert("This is line one\nThis is line two");
The above bit of JavaScript would output the following:

Reproducing With PHP
At first glance your initial thought on outputting the above using PHP might be to do something as follows:
echo "alert('This is line one\nThis is line two');";
Go ahead, give it a go and you’ll see that the new line is infact ignored and actually results in an error being generated.
The Solution
Solving this is easier than you might think. Simply add a second backslash before the existing one as shown below:
echo "alert('This is line one\\nThis is line two');";
Voila:

Follow us on Twitter
Subscribe to RSS Feed
Thanks! I didn’t come up with that idea. LOL :D
Thanks, this was helpful.