1

Possible Duplicate:
What is the canonical way to determine commandline vs. http execution of a PHP script?

I am writing a small script that will executed as a cron job. Is there a way to understand if this script is called from a web server so that I could echo <br /> instead of newline character as output?

Community
  • 1
  • 1
yasar
  • 12,122
  • 28
  • 87
  • 154

2 Answers2

13

From php.net

if (PHP_SAPI === 'cli') 
{ 
    // ... 
} 
djdy
  • 6,733
  • 5
  • 36
  • 62
0

If the code has been called from the command line, the server variable HTTP_USER_AGENT is not set. Something like this might assist:

$newline = (isset($_SERVER['HTTP_USER_AGENT'])) ? "<br />" : "\n";
BenM
  • 51,470
  • 24
  • 115
  • 164