Cara menggunakan php_eol example

🏠 » Q&A » How To Echo A Line Break / New Line In PHP

Table of Contents

  • Answer: In PHP to echo a new line or line break use ‘\r\n’ or ‘\n’ or PHP_EOL
  • Conclusion :
  • Reader Interactions
  • Use PHP_EOL to fwrite a newline in PHP
  • Other ways to write a newline to a file
  • How do I add a new line in PHP?
  • Can we use \n in PHP?
  • What is Br in 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

If HTML uses <br/> to create a line break, in PHP you can create a new line using 3 different methods :

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:

echo"Seegatesite.com\r\nThe best programming blog";

Result :

Cara menggunakan php_eol example

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:

echo"Create Line Break PHP".PHP_EOL."With PHP_EOL";

Result :

3. Using nl2br() 

Used to add HTML line breaks in a string (the HTML code we usually use to create new lines <br/>)

Example :

echo nl2br("Seegatesite.com \nsharing and learning programming");

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 Interactions

When writing to a file in PHP with fwrite, you can add a newline character with PHP_EOL. Using PHP_EOL instead of manually writing out a "\n" or "\r\n" is important to make your code as portable as possible.

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 PHP

Writing a newline to a file with fwrite() is very easy. In the below example, we’ll create a new file using fopen() and then add some text, a newline, then some more text using PHP_EOL:

<?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 fopen() function has two required arguments, the filename and the mode. “Mode” is the “w” we put after "foo.txt", and it means “open this file for writing”. If we just wanted to read the file, we would put “r” for “read”. Other options are:

  • r+ opens the file for both reading and writing
  • w+ also opens the file for both reading and writing
  • a stands for “append”, and opens the file for both reading and writing but starting at the end of the file
  • a+ same as above, but will create the file if it does not exist (which would then be the same as “w”)
  • e is a weird one, but basically it makes sure the file is closed if the process fork()s or exec()s. This prevents leaking file descriptors in some programs
  • c opens a file for writing and will create a file if it exists, but won’t truncate a file if it already exists
  • c+ opens a file for both reading and writing, then behaves the same as above
  • x creates a file and opens for writing; will fail if it already exists
  • x+ same as above but also opens the file for reading

But, we forgot something in our code! It’s important to remember to call fclose() on a file handle after you’re done with it. While it’s true that PHP will close the file handle for us automatically when the program finishes, it’s bad practice to rely on this, and could be very bad for large programs that run for a long time. It may take a while, but operating systems will eventually run out of file descriptors!

Below is the same code but with a proper fclose() at the end:

<?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

fwrite() is great and all, but it’s not the only way to write a newline to a file in PHP. You can also use the much more convenient function file_put_contents()! See below for a much shorter version of the above example:

<?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 file_put_contents() instead of fwrite(), you can avoid having to deal with file handles at all. You also don’t need to worry about closing the file when you’re done. It’s win-win! You do have to be careful though, this method will overwrite the file if you don’t tell it not to. See here for the file_put_contents() documentation.

Conclusion

We went over how to write a newline to a file in PHP with fwrite(), as well as a far superior method of writing to a file. We also covered the various options you can pass to fopen() to open files in different modes. Remember to use PHP_EOL as well to make your code as portable as possible!

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.