[VIDEO] How to double-click & run PHP in CMD/console?

Hello everyone,

Welcome to this short tutorial on how to run PHP codes in console or CMD.

You get PHP console with your PHP setup called php.exe which generally resides in your PHP folder. I have WAMP installed in my computer so I have PHP installed via WAMP.

First of all, you have to add the directory of your php.exe to the path environment variables. To do this go to the Advanced System Settings of your computer.

Bored with text? Watch this video to learn how to run PHP scripts through CMD with double-click

  1. You can either right click on My Computer and select properties and in the next window on the left side menu click on Advanced System Settings.
  2. Or you press Windows Key + R to bring the run window and type “control sysdm.cpl” (without quotes) and click on Advanced tab.

Anyway, here click on Environment Variables and then from the list of System Variables find and double click the variable Path. At the end of the text put a semicolon and paste the directory of your php.exe and close all the windows by clicking OK.

Here you are all set to use the PHP console through your CMD.

Now we will write our PHP script. I will create the index.php in my C: drive and putting the following lines of code in it

echo 5+10;

Now if I run this code, it should output 15.

So I run my CMD and type

php.exe C:\index.php

This actually mean, run the php.exe with the script at C:\index.php, thus finally it runs and outputs 15.

It’s done so far.

But it’s tedious to write php.exe every time as we generally need to run our script again and again to test. How about having a way to double click and run the script

Okay to do this create a batch file (*.bat) in the same location as your script in, and put the following lines of code in it

@echo off
php.exe index.php

Actually only the 2nd line is necessary. However the first line clears the CMD screen to empty. The 3rd line places a new line after the output of our script and the last line is important as it holds the CMD back from getting closed even before we see the output. If the ‘pause’ command is not given CMD will run the script, publish its output and then close itself off.

So from now on, you can simply copy & paste this batch file to accompany your script and it will work unless you have a different file name for the script other than index.php. Well in that case, simply edit that batch file and change the index.php to your script name.

I hope you like it. Let me know.

Google Recommends Recommends


Contact Us