Skip to content
PHP Logo

What best practices should be followed when converting line breaks in PHP to ensure proper formatting?

When converting line breaks in PHP, it is important to ensure that the formatting is consistent across different platforms. To achieve this, the PHP_EOL constant should be used to represent the correct line break character based on the operating system. This will ensure that the line breaks are properly formatted when outputting text.

php
// Convert line breaks using PHP_EOL constant
$text = "This is a sample text." . PHP_EOL;
$text .= "This text will have line breaks based on the operating system." . PHP_EOL;
echo $text;