Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Hide Buttons

How to run php from console for Windows

Most of the time, we deal with PHP for web­sites and web apps. In some cases we need our PHP code to run for non-ui tasks which got noth­ing to do with view or user inter­face. In such cases it’s handy to do a quick run of PHP file for test­ing pur­pose or to make a con­sole based php application.

This short tuto­r­ial assumes that we knows basics of PHP and it’s already installed. It explains it for Win­dows XP but the process should be same for other ver­sion of windows.

Step 1: Con­fig­ure the envi­ron­ment vari­able for php.exe file located within the php instal­la­tion direc­tory. It’s dif­fer­ent for each ver­sion of win­dows. It can be done for Win­dows Xp by fol­low­ing -> Start -> Win­dows -> right click on My Com­puter, click on Advanced Tab -> click on Envi­ron­ment Vari­ables But­ton -> choose path on Sys­tem Vari­ables -> click on Edit But­ton. Now add ; at the last and append the php instal­la­tion path to the direc­tory hav­ing php.exe file. It will add php to our sys­tem path.

Step 2: Now run the CMD by fol­low­ing -> Start -> Run … -> type CMD next to Open -> click on OK But­ton. It will open the com­mand prompt.

Step 3: We can check whether php is on sys­tem path by typ­ing php –ver­sion and hit enter. We might need to reboot our sys­tem in case we have freshly added php to envi­ron­ment vari­ables. It shows the ver­sion infor­ma­tion for php as men­tioned below:

E:\>php --version
PHP 5.4.16 (cli) (built: Jun  5 2013 21:01:46)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
E:\>

Step 4: Now write a php script using notepad or any other edi­tor of our choice. I wrote the php script at path E:\php\hello-php.php. The con­tent of this script was:

    echo "Hello PHP";

Step 5: Exe­cute the php script as shown below.

E:\php>php hello-php.php

It exe­cute our php script and print the out­put on con­sole as shown below:

E:\php>php hello-php.php
Hello PHP

This is how we can run php scripts from con­sole. This is help­ful for devel­op­ing con­sole based php applications.

You may also like...

1 Response

  1. September 2, 2014

    […] ← Previous […]

Leave a Reply

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