web analytics

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
echo.
pause

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.

Please follow and like us:
Pin Share

4 thoughts on “How to double-click & run PHP in CMD/console?”

  1. Hey Tanmay, really a nice, simple yet productive post. You being experienced in PHP and you are using WAMP also, I need you help. I am stuck up since days and cannot find any solution. Would you please help me!! Let me know.

  2. Actually, I had installed WAMP on Win 8 x64 PC which is now upgraded to Win 10. I can execute localhost, phpadmin, simple php script without any problems. Few days ago, a third party has written web services in PHP using MySQL. Php WS is based on REST API using CI REst API. The script is small using a single table from a Db & CRUD operations for it.
    Now, I somehow managed to execute the add script without errors. On executing POST to add record & providing proper params I get 200 status, but the Response & Request headers & all is just blank – no data in it. Whereas it should show me the added record confirmation. Even on executing GET it shows same response – though their are no records, but yet headers can't be blank.
    I see no errors or relevant log in apache, or mysql.

    What to do ?? The same code, the guy that provided code, is able to execute properly without any issues.
    It would be great if you can help me.

    Thanks

  3. You can record your screen and upload the video to youtube and give me a link to see everything you described in live action as operate.

    Or, if its doesn't matter, then send me the project with database structure filled with sample data.

    Anyway, I have to take a look.

Comments are closed.

RSS
Follow by Email
Scroll to Top