How to access PHP with the Command Line on Windows?

How to access PHP with the Command Line on Windows?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Configuring PHP CLI on Windows: Beyond the Default Installation

Running PHP scripts from the command line on Windows often feels like trying to assemble furniture without the instructions, especially when dealing with legacy systems or default installations. The core issue isn't that you can't run PHP; it's that the environment variables and executable paths are frequently misconfigured out of the box. When a user attempts to execute php version in CMD and receives a "not recognized" error, it almost invariably means the system cannot locate the php.exe binary or the necessary DLL files required to bootstrap the runtime.

The most common scenario involves a standard XAMPP or WAMP installation, where PHP is installed but not added to the system PATH. In these environments, the CLI tool exists in a subdirectory like C:\xampp\php, but Windows refuses to search there unless explicitly told to do so. You cannot simply run the script; you must navigate to that specific folder first. This friction highlights a fundamental architectural difference between Windows and Unix-like systems: Windows does not enforce a global search path for development tools as strictly, requiring manual intervention to bridge the gap between the GUI environment where Apache runs and the CLI environment where developers work.

To resolve this without reinstalling everything, you must modify your system's PATH variable. Open System Properties via the Control Panel, navigate to the "Environment Variables" section, and edit the Path variable for your user account. Add the full directory containing php.exe (e.g., C:\xampp\php) to this list. Once saved, open a new Command Prompt—refreshing an existing window won't pick up the changes—and verify the installation by typing php -v. If the version number appears, the basic connectivity is established.

However, simply finding the executable is only half the battle. PHP's CLI interface relies heavily on the presence of specific shared libraries, typically php_core.dll and php_mbstring.dll, located in the same directory as the executable. If these files are missing or corrupted, the script may fail to load even if the path is correct. This is a frequent pitfall when upgrading PHP versions via XAMPP; the GUI updater might change the version number but fail to replace the DLLs correctly, leading to fatal errors like PHP startup: unable to load internal extension.

When invoking scripts directly, you must ensure the file has a valid shebang equivalent or simply pass the path if using an absolute command. For instance, running a script located at C:\projects\script.php requires specifying the full path to PHP:

"C:\xampp\php\php.exe" "C:\projects\script.php"

Omitting quotes around paths containing spaces is a common error that causes Windows to truncate the command, resulting in syntax errors. Additionally, be mindful of the php.ini configuration file location. The CLI tool often looks for php.ini in the directory where it was executed or searches the system PATH. If your Apache and CLI are configured with different ini files (a common practice to isolate logging), you must explicitly specify the config path using -c:

"C:\xampp\php\php.exe" -c "C:\xampp\php\php.ini" "C:\projects\script.php"

This explicit configuration ensures that environment-specific settings, such as memory limits or error reporting levels, are applied correctly regardless of the current working directory.

For developers migrating from Linux to Windows or those maintaining legacy systems like Windows 7, understanding these environmental nuances is critical. The transition often requires patience, but once the PATH is set and dependencies verified, PHP CLI becomes a robust tool for automation and testing. If you find yourself needing a more structured environment where CLI and GUI configurations are managed seamlessly alongside modern framework support, exploring dedicated hosting or development platforms like https://laravelcompany.com can provide insights into managing complex deployment pipelines without getting bogged down in manual path configuration on every machine.

Our network