Non Brazilian Non Brazilian - 17 days ago 5
PHP Question

Not getting a new line when using use "\n" in PHP

I am trying to use

\n
in PHP to get a new line on my website, but it's not working.

Here is my code:

if(isset($_POST['selected'])){
$selected_val = $_POST['selected'];
// Storing Selected Value In Variable
echo "\n";
echo $selected_val; // Displaying Selected Value
}

Answer

When writing PHP code, you need to distinguish between two distinct concepts :

  1. go to the new line in the code you produce, which you do using "\n"
  2. go to the new line in the HTML webpage you produce, which you do using <br />

So, Option 1 makes you go to the new line in the code you produce, but you will not go to a new line in the HTML webpage you produce. The same way, option 2 makes you go to the new line in the HTML webpage you produce, but you will not go to a new line in the code you produce.

If you want go to the next line in both your code and the HTML output, you can just combine "\n" and <br /> :

echo "<br />\n";