PHP Detect if Script is running from CLI

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

If you have a script that you want to behave differently depending on whether it is called from the command line or via a http request then this little snippet of code is what you need.

    if(!defined('STDIN')){ //its not via command line
        // do non command line stuff
    }

Tags: phpCLIDetect