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';
}
Keywords
Related Questions
- How can the use of interfaces and abstract classes improve the design and functionality of PHP classes handling database connections and queries?
- What are some common techniques for manipulating and accessing JSON data within PHP scripts?
- What are some best practices for comparing dates in PHP when working with "datetime" fields?