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 websites and web apps. In some cases we need our PHP code to run for non-ui tasks which got nothing to do with view or user interface. In such cases it’s handy to do a quick run of PHP file for testing purpose or to make a console based php application.

This short tutorial assumes that we knows basics of PHP and it’s already installed. It explains it for Windows XP but the process should be same for other version of windows.

Step 1: Configure the environment variable for php.exe file located within the php installation directory. It’s different for each version of windows. It can be done for Windows Xp by following -> Start -> Windows -> right click on My Computer, click on Advanced Tab -> click on Environment Variables Button -> choose path on System Variables -> click on Edit Button. Now add ; at the last and append the php installation path to the directory having php.exe file. It will add php to our system path.

Step 2: Now run the CMD by following -> Start -> Run … -> type CMD next to Open -> click on OK Button. It will open the command prompt.

Step 3: We can check whether php is on system path by typing php –version and hit enter. We might need to reboot our system in case we have freshly added php to environment variables. It shows the version information for php as mentioned 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 editor of our choice. I wrote the php script at path E:\php\hello-php.php. The content of this script was:

    echo "Hello PHP";

Step 5: Execute the php script as shown below.

E:\php>php hello-php.php

It execute our php script and print the output on console as shown below:

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

This is how we can run php scripts from console. This is helpful for developing console 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 *