1

I'm trying to put 2 lines of sentences into a variable, which will be used to display a popup message (same one that shows using alert()). But when I try to put breaks between them the
tag instead just appears a part of text. Anyway to solve this issue? Below is my code.

$new_error_message = 'Oops, something went wrong! <br/> Your transaction could not be processed.';

Thank you

UPDATE:

I forgot to mention that I'm using 'throw new Phpr_ApplicationException($new_error_message);' to display the message because it's meant to be an error message, not alert().

Chris Bier
  • 14,183
  • 17
  • 67
  • 103
user3408005
  • 73
  • 1
  • 12

3 Answers3

2

Use the \n escaped character to create a new line in PHP.

$new_error_message = "Oops, something went wrong! \n Your transaction could not be processed.";

Also note that a string that uses escaped characters needs to be in double quotes.

Docs: http://www.php.net/manual/en/language.types.string.php

Chris Bier
  • 14,183
  • 17
  • 67
  • 103
  • Should probably use PHP_EOL constant instead. – Jessica Apr 30 '14 at 21:58
  • Can you post an example? That seems interesting and a valid way to address the issue – Chris Bier Apr 30 '14 at 21:59
  • Here's a good question talking about when to use `\n` and when to use `PHP_EOL` http://stackoverflow.com/questions/4975411/when-do-i-use-php-eol-instead-of-n-and-vice-versa-ajax-jquery-client-problem – Chris Bier Apr 30 '14 at 22:00
  • I'm not sure if you're using that to indicate use it or not, but that link does say for output, and this is output, yes? – Jessica Apr 30 '14 at 22:04
  • Yes, so it is indeed an option to accomplish the OP's task, and looks like a better option than I posted. Didn't know about it until you mentioned it though, so it's rightfully your answer. I won't post it as my own – Chris Bier May 01 '14 at 15:55
0

Instead of <br/> try \n.

TheHacksaw
  • 432
  • 2
  • 6
0

If it's an alert();, you want to replace the <br /> tag with \n

You can do this in javascript if you like (taken from here):

str = str.replace(/\n/g, '<br />');
Community
  • 1
  • 1
Duniyadnd
  • 4,013
  • 1
  • 22
  • 29