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
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';