Cross-Platform Line Break Echoing in PHP
When echoing line breaks in PHP across different operating systems, the choice between "n" and "r" can be confusing.
Difference Between n and r
Cross-Platform Solution
To ensure line breaks work consistently across all platforms, it's recommended to use the PHP_EOL constant. PHP_EOL is automatically set to the correct line break for the operating system where the PHP script is running.
PHP_EOL Usage
<code class="php"><?php echo "Line 1" . PHP_EOL . "Line 2"; ?></code>
Backwards Compatibility
For versions of PHP prior to 5.0.2, the PHP_EOL constant is not defined. In these cases, you can use the following code to determine the appropriate line break for your system:
<code class="php">if (!defined('PHP_EOL')) { switch (strtoupper(substr(PHP_OS, 0, 3))) { // Windows case 'WIN': define('PHP_EOL', "\r\n"); break; // Mac case 'DAR': define('PHP_EOL', "\r"); break; // Unix default: define('PHP_EOL', "\n"); } }</code>
The above is the detailed content of How to Ensure Consistent Line Breaks in PHP Across Different Operating Systems?. For more information, please follow other related articles on the PHP Chinese website!