Verification: a143cc29221c9be0

New line in php mysql

Use the nl2br() Function and the Newline \n Escape Sequence to Add the Line Break Within echo in PHP

We use the nl2br() function and the newline \n escape sequence to insert the line break within the echo statement in PHP. The nl2br() function inserts the HTML line breaks before the new line, and the \n escape sequence denotes the new line. The function returns the string with the
HTML tags. For example, use the nl2br() function in the echo statement. Write a string sun inside the nl2br() function and use the \n escape sequence. Write another string, moon, right after the escape sequence.

Do not get confused with /n as \n. Firstly we write the backward slash and then n. Make the use of double quotes for the parameters inside the nl2br() function. The use of single quotes results in the printing of the exact parameter written in the function. Visit the PHP manual to learn about the nl2br() function.

Example Code:

# php 7.*



Use the File Handling Method and Its Functions to Add the Line Break in PHP

We can use the file handling functions like fopen(), fwrite(), and file close() to create a file and write some text in it. We use the \n escape sequence as in the first method. As a result, the text written after the \n will appear in the next line in the file. We use the fopen() function to create a new text file in write mode. We use the fwrite() function to write the particular text to the created file. The flclose() function closes the file stream.

For example, create a variable $file and assign the fopen() function to it. Inside the function, write the first parameter as test.txt, which is the file to be created, and write w+ as the second parameter to denote the write mode. Write a string as Sun \n Moon in a variable $text. Use fwrite() function and pass the $file and $text variables as the parameter. Close the file stream using the fclose() function with the $file as the parameter.

The example below creates a text file test.txt in the root directory when the script runs in the server. Then, it inserts the texts into the file. We can view the output by opening the text file. Have a look at the PHP manual to learn about the file operations.

Code Example:

#php 7.x