PHP on Windows

Running .php files from the context menu

In a GUI-driven operating system like Windows, where you don't hang around the command line that much, it is sometimes useful to run a PHP file from its context menu in Windows Explorer.

That's easy to set up. But a reasonably smart handler should perhaps not hand the file over to PHP blindly. PHPUnit test cases need a different treatment, so the handler could try to detect them and pass them on to PHPUnit instead.

Here's a simple script, php-cli.bat, that does just that. Test files are identified through convention, not introspection: If the filename ends in "...test.php", PHPUnit will get to handle it. All other files are passed on to PHP. Both PHP and PHPUnit are expected to be in your PATH.

This is what the script does:

@REM PHP CLI runner, incorporating a PHPUnit test runner.
@REM o Expects a filepath as first argument.@REM o An ordinary file will be passed on to PHP. If the file is a test, it@REM will be run through PHPUnit.@REM o Test files are identified by their basename ending in 'Test'.@REM o PHP and PHPUnit must be found in the PATH.@echo off
set separator=________________________________________________________________________________
@REM Get the filename as basename without extensionset filename=%~n1set filepath=%~f1set scriptdir=%~dp1cd /d %scriptdir%
@REM check if the filename ends in "Test"set suffix=%filename:~-4,4%if "%suffix%" == "Test" GOTO :IS_TESTFILE
@REM not a testfile, run through PHPecho Running %filepath% ...
echo%separator%echo.
php %*echo.
@REM keep the window open after PHP has run
cmd /K
GOTO :END
:IS_TESTFILE
@REM this is a testfile, use PHPUnitecho Running tests in%filepath% ...
echo%separator%echo.
START /B phpunit %*
:END

Setting it up

There are several ways to add the batch script to the context menu of .php files.

In the "Open with ..." flyout menu (Windows 7)

Just put the batch file wherever you like it to be "installed". Next, right-click on any PHP file, select "Open with ..." > "Choose default program" > "Browse" and pick the script. Before you hit OK, don't forget to uncheck "Always use the selected program to open this kind of file" unless that's really what you want.

As soon as this is done, php-cli.bat will become a permanent option in the "Open with" list for PHP files.

At the top level of the context menu (Windows 7)

You'll have to edit the Registry for that. (Disclaimer: Don't do that unless you know what you are doing, are aware of the risks involved and take adequate precautions.)

Assuming you have registered .php files before as a PHPFile type (or some app has done it for you),

open HKEY_CLASSES_ROOT\PHPFile\shell\

Add a subkey named "Run with PHP".

Set the default value to the text you want to appear in the context menu, e.g. "Run with PHP"

"cooool thx for sharing!!!!!", haha. Yeah this is a nice script. I don't have any need for phpunit, but it's nice to use for debugging php cron scripts(especially long ones).
The reason I find this much more useful than the browser, is because in the browser it buffers the stdout until the page is done, then flushes it all to the page. If you run it from the command line you get every echo as it happens.
You can use flush(), but if you plan on adding header data down the road in the script, it'll break that.
Anyways thanks, this helps.