nl2br php function, when to use it

I just got a simple thought about PHP function…

When you should use nl2br to make text line breaks:

  1. Before inserting data to db(MySQL).
  2. After, retrieving the data from db.
  3. Doesn’t matter.

I thought that using a PHP function like nl2br every time when retrieving the data from db will put more pressure to the server (CPU), or not?

Thank you!

#2

Any transformation for the output medium (that includes any HTML escaping or converting newlines to HTML) should happen at the latest possible moment, preferably right as you’re outputing to the HTML page.

2 Likes

@Jeff_Mott is absolutely correct.

It may also be helpful to know that the nl2br function doesn’t actually convert a newline character to a <br>. It simply inserts the <br> characters into the string (prior to the newline character), leaving the newline character intact (helpful to know especially e.g. when output is to be interpreted as a javascript string).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.