We’ve all seen them:
- \n – new line
- \r – carriage return
- \t – tab
- \b – backspace
But many wonder when to use them or more specifically, why they aren’t working as expected. So let’s address the basic usage and rules.
Rule #1: When using a formatting character in your code, it must be within “double quotations” otherwise it will be taken as a literal backslash and letter.
When do you use it? When writing to a file with fwrite() or file_put_contents(), sending a text email with mail(), or when adding formatting to pre-populated data in the form element
Rule #2: Not all computer systems obey the formatting characters the same. When using \n (new line), also include a carriage return (\r) character.
So what do you do if you have a paragraph, for instance submitted by a
Example:
echo nl2br("Hello\n\rWorld\n\r!!!");
Results:
Hello
World
!!!