🏠 » Q&A » How To Echo A Line Break / New Line In PHP Show Table of Contents
Answer: In PHP to echo a new line or line break use ‘\r\n’ or ‘\n’ or PHP_EOLIf HTML uses 1. PHP new line character ‘\n’ or ‘\r\n’ ‘\n’ or ‘\r\n’ is the easiest way to embed newlines in a PHP string. Also, ‘\n’ can be used to create line breaks when creating PHP text files. See the following example:
Result : What is the difference ‘\n’ or ‘\r\n’ ? \n Used to write a new line on a UNIX system, while Windows reads a new line with the \r\n character. 2.PHP_EOL: PHP Core Predefined Constants PHP_EOL is predefined constants to translate the end of a line in PHP. Source : PHP Manual Example:
Result : 3. Using nl2br() Used to add HTML line breaks in a string (the HTML code we usually use to create new lines Example :
Result : Conclusion :The three methods above used according to your needs in creating new lines in PHP. This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress. Reader InteractionsWhen writing to a file in PHP with What is portability? Portability means you can run your PHP script on Windows or a Unix-like system (Linux, Mac, etc.) without changing the code. It’s important to have portable code to minimize bugs as well as lessen the work future-you has to do! Use PHP_EOL to fwrite a newline in PHPWriting a newline to a file with <?php $file_handle = fopen("foo.txt", "w"); fwrite($file_handle, "This is some text"); fwrite($file_handle, PHP_EOL); fwrite($file_handle, "This is some more text"); fwrite($file_handle, PHP_EOL); ?> In the above example, we first open the file using fopen(). The
But, we forgot something in our code! It’s important to remember to call Below is the same code but with a proper <?php $file_handle = fopen("foo.txt", "w"); fwrite($file_handle, "This is some text"); fwrite($file_handle, PHP_EOL); fwrite($file_handle, "This is some more text"); fwrite($file_handle, PHP_EOL); fclose($file_handle); ?> Great! Now if we run this, the file contents should look like the following: This is some text This is some more text Other ways to write a newline to a file
<?php $my_text = "This is some text".PHP_EOL."This is some more text".PHP_EOL; file_put_contents("foo.txt", $my_text); ?> If you use ConclusionWe went over how to write a newline to a file in PHP with How do I add a new line in PHP?Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string. Can we use \n in PHP?It is an in-built function of PHP, which is used to insert the HTML line breaks before all newlines in the string. Although, we can also use PHP newline character \n or \r\n inside the source code to create the newline, but these line breaks will not be visible on the browser. What is Br in PHP?Using Line Breaks as in HTML: The <br> tag in HTML is used to give the single line break. It is an empty tag, so it does not contain an end tag. Syntax: </br> Example: PHP. How do you end a line in PHP?Answer: In PHP to echo a new line or line break use '\r\n' or '\n' or PHP_EOL. |