This simple function will return true if the PHP script is running from the command line interface (CLI) or false if PHP is being executed via a different interface such as over HTTP with an Apache, Nginx, or Windows server. It utilizes the php_sapi_name PHP function which returns the type of interface between a web server and PHP.

This helper function can be useful for many different circumstances, but a common scenario is for determining how to format the results of your script. For example if PHP is executing via the CLI you may want to use simple text formatting and colors, but via a web page you may actually want to output HTML.

 * Is PHP running from the command line interface (CLI)?
 * @return bool Returns true if its the CLI, false otherwise
function isCli(): bool
    if (strpos(php_sapi_name(), 'cli') !== false) {
        return true;

    return false;

This code snippet was published on It was last edited on



  • Votes
  • Oldest
  • Latest