Many times it is necessary to determine if the php script is running on the command prompt. Recently I wrote a php shell script which could also be run from a browser and needed to decide in the code if to output a
'\n' or a ‘html break’ after a text line.
The following simple function returns a true if the script is run from a command prompt and false otherwise.
It uses the php_sapi_name() function, which returns a string that describes the type of interface PHP is using. The possible return values of the php_sapi_name() function include: aolserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames.