2

I've written some PHP which encases some javascript within a PHP variable, like so:

$js .= 'response['.$x.'] = FinanceDetails("'.$finance_code.'",' . $cart_value . ','.$minimum_deposit.','.$total_cost.');';

But this can be repeated up to three times, and then it's injected between <script> tags and I'd like it to show like so:

response[0] = FinanceDetails("ONIF12",290.00,10,1500);
response[1] = FinanceDetails("ONIF6",290.00,10,1500);
...

But currently it shows in one long string as there are no line breaks. I've tried adding \n and \r as well as \n\r together at the end of the $js value but I just keep getting an Illegal character error - how would I implement a new line / line break in this situation?

2
  • php.net/manual/en/function.nl2br.php Commented Apr 20, 2016 at 10:45
  • @RayonDabre nl2br won't work, since he is writing out javascript code. The code would just contain <br> then, which is invalid javascript. Commented Apr 20, 2016 at 11:02

1 Answer 1

2

You have to use the "" quotation marks instead of '' to be able to print out newlines. For example:

$js .= 'response['.$x.'] = FinanceDetails("'.$finance_code.'",' . $cart_value . ','.$minimum_deposit.','.$total_cost.');' . "\n";

Otherwise PHP does not parse the newline and your code ends up with a "\n" in it.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect answer - had a funny feeling I'd seen something similar before with the single / double quote, thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.