How are \r \t and \n different from one another?

The characters "\r" (carriage return), "\t" (tab), and "\n" (newline) are all special characters used in strings in the PHP programming language.

  • "\r" represents a carriage return, which is a control character that moves the cursor to the beginning of the line.
  • "\t" represents a tab, which is a control character that moves the cursor a certain number of spaces to the right.
  • "\n" represents a newline, which is a control character that moves the cursor to the next line.

Watch a course Learn object oriented PHP

These characters are used to format text and are often used in combination with one another to create well-formatted output.

<?php
$text = "Line 1\rLine 2\tIndented line 2\nLine 3";

echo $text;
?>

Output:

Line 1
Line 2	Indented line 2
Line 3

As you can see, the \r character returns the cursor to the beginning of the line, overwriting any previous content on that line. The \t character adds a tab, and the \n character starts a new line.