in this quick PHP tutorial, We’ll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an end-of-line character. It helps in dealing with text file I/O operations in PHP.
Linux and macOS represent the end-of-line marker by the newline character (\n
), whereas Windows environments are represented by a combination of two characters a newline (\r\n
).
Also checkout other related tutorials,
Merge Two Array or Multiple Array in PHP
How To Convert XML To Associative Array in PHP
Remove Duplicates From Multidimensional Array
How To Convert XSD into Array Using PHP
There are the following end-line characters used by the platform:
\n
) to represent the end of a line.\r\n
) sequence.\r
) character.Let’s write a file that has two dummy lines and will use PHP_EOL, The PHP_EOL will add end-of-line marker based on the Operating system where the script is running.
$file = fopen('phpflow.txt', 'w'); fwrite($file, 'Line 1' . PHP_EOL); fwrite($file, 'Line 2' . PHP_EOL); fclose($file);
The above code will write two lines of text to a file named 'phpflow.txt'
.
We can also use PHP_EOL constant to concatenate ‘Hello’ and ‘World’ with the appropriate line break between them.
$message = 'Hello' . PHP_EOL . 'World'; echo $message;
We can also use php_eol
constant with heredoc
and newdoc
.
$text = <<<EOT This is a multiline string. It spans multiple lines. And ends here. EOT; echo $text . PHP_EOL;
We have discussed different scenarios to apply PHP_EOL with PHP application. You can add an end-line marker into your string into a text regardless of platform. You can create strings/file by using php_eol
and it’ll apply end-of-line characters based on PHP platform environment. It helps to write cross-platform code that helps end-of-line endings.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More