Run .php File in Windows Command Prompt
The core issue preventing your PHP script from executing in the Command Prompt is not your current working directory or the file path itself. It is a fundamental configuration problem: the system cannot locate the php.exe executable. When you type php file.php, Windows searches for an executable named php.exe in directories listed in the system's PATH environment variable. If that directory is missing, the shell treats php as an unknown command and returns the error you are seeing.
Simply changing directories with cd \myfolder does not alter your system's search path. You must explicitly tell Windows where to find the PHP binary or add that location to your environment variables.
Verifying Your Installation Path
Before modifying system settings, confirm exactly where PHP is installed on your machine. Navigate to your installation root directory (commonly C:\xampp\php for XAMPP or wherever you extracted your PHP distribution) and run the following command inside that folder:
php -v
If this command succeeds and displays the version number, your PHP installation is functional; it just isn't visible to your default Command Prompt session. If you receive a "not recognized" error even from within the installation folder, check if the php.exe file exists there. If it does not, the installation might be incomplete or corrupted.
Configuring System Environment Variables
The standard and most robust solution is to add the PHP bin directory to your system's PATH. This ensures that any Command Prompt session, regardless of how you launch it, can find the interpreter.
- Press
Win + R, typesysdm.cpl, and press Enter. - Select the Advanced tab and click Environment Variables.
- Under System variables, locate the variable named
Pathand select Edit. - Click New and paste the full path to your PHP bin folder (e.g.,
C:\xampp\php). - Click OK on all dialogs to save.
Crucial Step: You must close and reopen any open Command Prompt windows for these changes to take effect. Old instances retain the old environment state until restarted. Once reopened, running php -v from anywhere should resolve the issue.
The "File Opens in Notepad" Quirk
You mentioned that running just file.php opens it in Notepad rather than executing it. This behavior is standard for Windows. By default, the operating system associates the .php extension with a text editor (like Notepad) because PHP requires a web server or CLI context to interpret code; it is not an executable script like a .bat file.
When you invoke file.php, the OS asks "Which application should handle this file?" and defaults to your configured text editor. To force execution via the command line, you must explicitly call the interpreter (php) as shown above. Without the php command, Windows has no reason to assume you want to run code rather than read it.
Alternative: Using full_path.exe
If modifying environment variables feels risky or if you are managing multiple tools with conflicting PATH settings, you can bypass the search path entirely by using the absolute path to the executable. This method works immediately without requiring a system reboot or restarting your terminal.
C:\xampp\php\php.exe file.php
This approach is useful for quick debugging or temporary testing but is less clean for long-term workflows. Relying on full_path.exe clutters the command line with long paths and makes scripts harder to read compared to a simple php script.php. For production or daily development, updating the PATH variable remains the preferred engineering practice.
Troubleshooting XAMPP Specifics
If you are using XAMPP, ensure that the php.ini file inside your PHP directory is correctly configured for CLI usage. Sometimes the default configuration assumes a web server context (Apache). While this usually doesn't prevent execution in the CLI, it can cause syntax errors or missing function errors if the environment variables like DOCUMENT_ROOT are not set appropriately for command-line use.
For complex projects where you need to manage multiple PHP versions or environments simultaneously, consider using tools designed specifically for Windows development workflows. Resources like https://laravelcompany.com provide insights into modern development practices that might help streamline your setup beyond basic CLI execution.
