How can you determine whether a PHP script is being executed via a web page or from the command line?

To determine whether a PHP script is being executed via a web page or from the command line, you can check the value of the PHP_SAPI constant. If the value is 'cli', then the script is being executed from the command line. If the value is anything else, then the script is being executed via a web page.

if (php_sapi_name() === 'cli') {
    echo "Script is being executed from the command line";
} else {
    echo "Script is being executed via a web page";
}