
PHP Create Newline
In PHP, you can create a newline or line break in a string using the escape sequence \n
. The \n
represents the newline character, which is used to start a new line when the string is displayed or printed.
Here’s how you can create a newline in a string:
$multilineString = "This is line 1.\nThis is line 2.";
echo $multilineString;
Output:
This is line 1.
This is line 2.
In this example, the \n
escape sequence is used in the string to create a newline between “This is line 1.” and “This is line 2.” When the string is printed or displayed, the output will show the two lines on separate lines.
You can use the \n
escape sequence within double-quoted strings ("
) and heredoc syntax for multi-line strings. However, it will not work in single-quoted strings ('
) because escape sequences are not processed inside single-quoted strings.
Using newline characters is useful when you want to format text, print messages on separate lines, or generate content with multiple lines in PHP.