PHP CLI (Command line Interface)

Mar 23, 2015 code

Im sure you’ve see other tools that you type in command line options or commands and it does a lot of things in the background for you. Things like node.js or even Ruby both use┬áheavy command line interfaces.

How can you do this with PHP? It’s actually quite simple! All you have to do is put your php.exe path into your environment variables and you are good to go! This guide is for a Windows Machine.

Fastest way(Im using xampp but you would point it where your php.exe directory exisit):
Open your command prompt and type it in (keep spacing as is).

SET PATH=%PATH%;C:\xampp\php

Alternatively you can:
Right click on your computer or mypc > properties
On the left click Advanced system Settings.

Then go to Advanced tab and Environment Variables.


Then add to your variables with a ; acting as the add sign. Include your path to your php.exe


Click okay and open a command prompt up. Type:

php -v

This should give you the version of PHP you are using with a Zend Engine version as well. Note if you get an Zend eAccelerator error, refer to this page once you find your version of PHP drop the file into your php folder (where php.exe resides) and if the file name is different you can edit the file location in your php.ini file.

Once up and running you can now use PHP as a local script for doing anything you want.

To run a script just use (note you either need to be in the directory where the php file is or specifiy the path as C:\folderpath\scriptname.php)

php scriptname.php

For instance I have a script that helps with my WordPress workflow when I start a project. It gets the wordpress zip core, unzips it into a folder I specify as an argument in the command line. Then it grabs a starter theme I use for all my projects (a blank theme) and adds it to the theme folder, next it gets a set of plugins that I use for most of my projects and puts it in the plugins folder. All with one line of code, and I don’t have to copy and paste things. Note all these files are on my local machine so the script just moves and creates files and directories for me. Next I plan on having the script create a database and write to my config file for me, start my server and launch the install page. The possibilities are endless!