Launch a .php script in Windows by double-clicking

Launch a php script in windows by double-clicking

PHP is a great programing language for the web, but it can also be a handy command line interface (CLI) scripting language.

You may have PHP running in Windows either as a specific installation of PHP or perhaps as part of WAMP (i.e. Windows – Apache – MySQL – PHP).

This article describes how to execute a CLI PHP script by simply double-clicking on the script icon from the Windows interface.  Here is how to set it up.

Step 1: Put PHP in the Windows Environment Variables

This step allows you to run a PHP script from the command line by typing:

php myscript.php

instead of something like:

C:\wamp\bin\php\php5.4.12\php myscript.php

You need to know the path to your CLI PHP executable.  The example above is where it is located in a typical WAMP installation.  Note that in WAMP, there are three PHP executables (php.exe, php-cgi.exe, and php-win.exe).  You want php.exe.

Once you know the path to php.exe, you add it to the Windows Environment Variables.

Right click on your computer icon and select Properties.

Step1-1
Select Properties

Select Advanced system settings

Step1-2
Select Advanced system settings

Press the Environment Variables … button

Step1-3
Press Environment Variables …

In the System variables window, scroll down until you find the Path variable.  Select Path and then press the Edit … button.

Step1-4
Select Path variable, then press Edit …

In the Variable value: window, scroll all the way to the right.  Type a semicolon (;) and then the full path to the directory where your php.exe file is located.  Note the trailing backslash (\).

Step1-5
Enter path to the directory containing php.exe

Press the OK button  >  OK button  >  OK button.

Now you can open a DOS window and type:

php myscript.php

Note that you must be in the same directory as myscript.php or else specify the full (or relative) path to the script.

 

Step 2: Associate the .php extension with php.exe

When you double click on a php script, you want it to open up a DOS window and then start executing.  We accomplish this by associating the .php extension with php.exe.

Right Click on any file with a .php extension.

Open with  >  Choose default program …

Make sure that the box in front of “Always use the selected program to open this type of file” is checked.

Step2-1
Make sure Always use … is checked

Click the Browse button.

Browse to the location of php.exe – the same path that you specified in Step 1 above.

Click on php.exe and press the Open button.

This will open a DOS window and launch the php script in this window.  Also, every time in the future that you double click a script with a .php extension, it will also launch in a DOS window.

 

Step 3: Set the default DOS window size

The DOS window that opened may not be big enough or include enough of a scroll back history.  You can set the default for the DOS window that will be shown every time you launch a DOS window.

Launch a DOS window by:

Windows Menu  >  All Programs  >  Accessories  >  Command Prompt

Right click on the small C:\ icon in the upper right corner and select Defaults.

Step3-1
Select Defaults

I only changed the settings on the Options and Layout tabs.  Here are the defaults that I use:

CMD Defaults Options
Options tab
CMD Defaults Layout tab
Layout tab

Summary

That’s all there is to it.  Note that the DOS window will close when the script finishes execution, so you probably will want to add a “Press enter to continue” routine at the end of your script.

Try creating the following script and then double clicking on it.

<?php
echo "Hello world";

echo "\n\n";
echo "Press ENTER to continue... ";

$handle = fopen("php://stdin","r");
$getLine = "";
while ($getLine == ""):
     $getLine = fgets($handle);
endwhile; 

exit(0);
?>

If you found this article helpful, please leave a reply below.  Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *