How can PHP scripts be designed to work both as cron jobs and in regular browser requests?

To make PHP scripts work both as cron jobs and in regular browser requests, you can check if the script is being run from the command line or as a web request. This can be done by checking the PHP_SAPI constant. If PHP_SAPI is equal to 'cli', then the script is being run from the command line and you can execute the necessary cron job logic. Otherwise, the script is being accessed via a browser and you can execute the regular web request logic.

if (PHP_SAPI === 'cli') {
    // Code for cron job logic here
    echo "Running as a cron job\n";
} else {
    // Code for regular web request logic here
    echo "Running in a browser\n";
}