Soumalya Banerjee
Soumalya Banerjee

Reputation: 1966

PHP_EOL is not working on mac

Can anybody tell me, why PHP_EOL is not working on my Mac?

Code:

echo 'This is first line'.PHP_EOL.'This is second line';

Can any body help me through this?

Upvotes: 2

Views: 865

Answers (2)

Alessandro
Alessandro

Reputation: 898

Sometime you can find this constant undefined, modern hosting with latest php engine do not have this problem but I think that a good thing is write a bit code that saves this situation:

<?php
  if (!defined('PHP_EOL')) {
    if (strtoupper(substr(PHP_OS,0,3) == 'WIN')) {
      define('PHP_EOL',"\r\n");
    } elseif (strtoupper(substr(PHP_OS,0,3) == 'MAC')) {
      define('PHP_EOL',"\r");
    } elseif (strtoupper(substr(PHP_OS,0,3) == 'DAR')) {
      define('PHP_EOL',"\n");
    } else {
      define('PHP_EOL',"\n");
    }
  }
?>

So you can use PHP_EOL without problems... obvious that PHP_EOL should be used on script that should work on more systems at once.

PHP_EOL means:

1) on Unix    LN    == \n
2) on Mac     CR    == \r
3) on Windows CR+LN == \r\n

Hope this answer help.

Upvotes: 1

deceze
deceze

Reputation: 522016

I'm just going to guess that you're looking at the result in your browser, where whitespace is collapsed. PHP may output this:

This is first line
This is second line

But the browser renders it like this:

This is first line This is second line

To insert an explicit line break in HTML, use the <br> tag.

Upvotes: 8

Related Questions