PHP is not only a powerful scripting language for web applications, but it can also be used to run command line scripts. This can be useful for running cron jobs, quickly testing a function, or running scripts which don’t need to be accessed via a web interface.

First, we need to set the path to the PHP executable, which we will use to run our script. This is found where PHP is installed. On Windows, we can set the path by opening the command console and typing:

PATH = %PATH%;path-to-php-executable;

This operation appends the path to the executable to the existing PATH variable.

Next we will edit our php.ini file to make use of short tags. Open up your version of PHP’s php.ini file (usually found where PHP is installed), and search for ‘short_open_tag’, and ensure it says ‘short_open_tag = On’. If it is off, change it to on. This simply allows us to use ‘‹?’ instead of the full PHP open tag ‘‹?php’.

Now we will create a new php file, called ‘myScript.php’. Insert the following code:

‹? echo ‘Hello World! This is my first command line script’; ?›

Open up your command console in the directory where ‘myScript.php’ resides (tip: on Windows, you can do this by holding ‘Shift’ and right clicking on the directory folder and selecting ‘Open command window here’). We can run the script by typing into the console:

php myScript.php


There are some really handy options with the PHP executable. For example, you can check solely for syntax errors by running ‘php -l myScript.php’. Type ‘php -h’ for a full usage list.

  • Simon

    I’ve googled this article about once per week for the last year. You think I’d know how to change my PATH from the command line by now =)

    Thanks team!

We'd love to
hear from you

We've prepared a simple project planner to get started.

Lets get started

Or send us a

Phone: (07) 3878 1151

Address: Office 2, 59 Hardgrave Rd,
West End, Brisbane, 4101, QLD, Australia

  • This field is for validation purposes and should be left unchanged.