HTML和CLI的换行符?

I have a script which can be called both from browser and CLI. It produces output, so adding a new line is a must. But, if youre viewing from a browser, the .php should use <br> while watching from CLI its the or PHP_EOL:

echo 'output1';
if $browser
{
    echo '<br>';
}
else
{
    echo "
";
}
echo 'output2';

Isnt there any universal character?

Versions of PHP binaries could vary, for example on servers that have fastcgi the php binary might point to php-cgi.

So to test cli in an interface independent manner checking the contents of the $_SERVER variable for example is a more preferred way.

I think this would handle all the checks from whether the script is run from

  1. Command line
  2. As a cron job
  3. PHP binary
  4. Browser
function is_cli() {
  return ((empty($_SERVER['REMOTE_ADDR']) and ! isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0) ||defined('STDIN')) ? true : false;
}

echo 'output1';
if (is_cli()) {
  echo "
";
} else {
  echo '<br>';
}
echo 'output2';