Running PHP in CLI with WAMP

Sometimes it’s useful to run PHP scripts via a CLI (mostly when doing long-running scripts). If you’re on Windows and using WAMP, you already have a CLI version available.

Just point your terminal to php.exe which is typically located under C:\wamp\bin\php\php-5.3.0\php.exe and use the -f flag to indicate that you want to run a file. So, to run PHP under CLI:

C:\wamp\bin\php\php-5.3.0\php.exe -f C:\wamp\www\index.php

Detecting if PHP is running under CLI

Here’s a simple function that checks if PHP is running via CLI or web:

function is_cli()
    return php_sapi_name() === 'cli' && empty($_SERVER['REMOTE_ADDR']);

  • Shiva Kumar

    Thanks a lot, this helped me