PHP Create Newline
In PHP, we can create a new line inside a string using special characters.
Syntax
echo "string1\nstring2"
Here, "\n"
is the special character used to create a new line.
Example
Let's take an example where we want to print a name and address on separate lines.
$name = "John Doe";
$address = "123 Main Street, Anytown USA";
echo "$name\n$address";
Output
The above example will produce the following output:
John Doe
123 Main Street, Anytown USA
Explanation
In the above example, we have defined two variables $name
and $address
with the name and address of a person. We have then used the echo
statement to print the values of these variables with a new line character "\n"
in between them. This creates a new line after printing the name on the first line and the address on the second line.
Use
The "\n"
character can be used to create a new line in any PHP string. This is useful when we need to print multiple lines of text or when we need to format the output of our scripts.
Important Points
- The
"\n"
character is not visible in the output and only creates a new line. - The
"\n"
character can only be used inside a string and not as a standalone statement. - There are other special characters that can be used to format text, such as
"\t"
for tabs or"\r"
for carriage returns.
Summary
In PHP, we can use the "\n"
special character to create a new line inside a string. This is useful when we need to print multiple lines of text or format the output of our scripts. The "\n"
character is not visible in the output but creates a new line. Other special characters can be used for formatting text as well.