Search code examples
javascriptphpstringline-breaks

<br> between two variables in javascript with PHP


I create with php, a JavaScript file where inside there are two variables.

I want that two variables are on two different lines, maybe using a tag

The problem is that the output on the browser works, but inside the file.js the two variables are on the same line, and between a variable and the other there is
(in text)

$javascriptContent = json_encode($var);
$settimana = "hello";
$javascriptContent = 'var settimana= "'. $settimana .'"; <br>  var reports = '. $javascriptContent . ";";
file_put_contents($path, $javascriptContent);

works only on browser in the file no

I tried also with:

<br />
\n 

nut nothing :(


Solution

  • JavaScript doesn't have HTML tags. It just has JavaScript code. What you're looking for is a newline character, not an HTML tag. Something like this:

    'var settimana= "' . $settimana . '";' . "\r\n" . 'var reports = '. $javascriptContent . ";"
    

    (Note the use of a double-quoted string to interpret the newline, as a single-quoted string won't interpret it and just treat those as literal characters.)