Skip to content
PHP Logo

Are there any best practices for handling line breaks in PHP output?

When outputting text in PHP, it's important to consider how line breaks are handled. To ensure consistent formatting across different platforms, it's recommended to use the PHP_EOL constant to represent the correct line break character for the current platform. This will ensure that line breaks are displayed correctly regardless of whether the code is running on a Windows, Unix, or Mac system.

php
<?php
// Using PHP_EOL to handle line breaks
$text = "This is a line of text." . PHP_EOL;
$text .= "This is another line of text." . PHP_EOL;
echo $text;
?>