What is the difference between executing a PHP script via CLI and via the web?

When executing a PHP script via CLI (Command Line Interface), the script runs in a terminal window without any web server involvement. This means that certain variables like $_SERVER['HTTP_HOST'] or $_SERVER['REQUEST_URI'] will not be available when running via CLI. To address this difference, you can use conditional statements to check if the script is running via CLI or the web server, and set appropriate values for these variables accordingly.

if (php_sapi_name() == 'cli') {
    $_SERVER['HTTP_HOST'] = 'localhost';
    $_SERVER['REQUEST_URI'] = '/cli_script.php';
}

// Your PHP script code here