Saturday, May 4, 2024
HomePHPWhat's the Objective of php_eol in PHP?

What’s the Objective of php_eol in PHP?


on this fast PHP tutorial, We’ll talk about php_eol with examples. PHP_EOL is a predefined fixed in PHP and represents an end-of-line character. It helps in coping with textual content file I/O operations in PHP.

Linux and macOS symbolize the end-of-line marker by the newline character (n), whereas Home windows environments are represented by a mixture of two characters a newline (rn).

Additionally checkout different associated tutorials,

Merge Two Array or A number of Array in PHP
How To Convert XML To Associative Array in PHP
Take away Duplicates From Multidimensional Array
How To Convert XSD into Array Utilizing PHP

There are the next end-line characters utilized by the platform:

  • Unix-based techniques (e.g., Linux, macOS) sometimes use the newline character (n) to symbolize the top of a line.
  • Home windows makes use of a mixture of newline (rn) sequence.
  • Basic Mac OS (pre-OS X) traditionally used solely the carriage return (r) character.

Writing to Recordsdata:

Let’s write a file that has two dummy traces and can use PHP_EOL, The PHP_EOL will add end-of-line marker based mostly on the Working system the place the script is working.

$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 traces of textual content to a file named 'phpflow.txt'.

Concatenating Strings

We will additionally use PHP_EOL fixed to concatenate ‘Hey’ and ‘World’ with the suitable line break between them.

$message = 'Hey' . PHP_EOL . 'World';
echo $message;

PHP_EOL with Heredoc or Nowdoc Syntax:

We will additionally use php_eol fixed with heredoc and newdoc.

$textual content = <<<EOT
It is a multiline string.
It spans a number of traces.
And ends right here.
EOT;
echo $textual content . PHP_EOL;

Conclusion

Now we have mentioned completely different situations to use PHP_EOL with PHP software. You possibly can add an end-line marker into your string right into a textual content no matter platform. You possibly can create strings/file through the use of php_eol and it’ll apply end-of-line characters based mostly on PHP platform atmosphere. It helps to write down cross-platform code that helps end-of-line endings.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments