How can you determine if a PHP script is running from the command line or user interaction?

To determine if a PHP script is running from the command line or user interaction, you can check the value of the PHP_SAPI constant. If the PHP_SAPI constant is equal to "cli", then the script is running from the command line. If it is not equal to "cli", then the script is running from user interaction.

if (PHP_SAPI === 'cli') {
    echo 'Running from command line';
} else {
    echo 'Running from user interaction';
}