Running PHP From Windows Command Line

//Running PHP From Windows Command Line

php windows
Since PHP is a server side scripting language, a lot of development using PHP is done on a live web environment. However, PHP does allow you to install it and run scripts on your local machine with no web access needed.

Running PHP from Windows command line can be especially useful when trying to rapidly develop custom scripts, or to read and modify local files. In the example, we will use a custom PHP iterator.

BASIC CONFIGURATION

1. First you will need download the latest version of PHP and extract it to your C:\ directory.

https://windows.php.net/download#php-5.6

php step one

 

2. Navigate to Control Panel > System and Security > System > Advanced system settings. In the System variables panel click, find and edit the Path variable and append the path to your extracted PHP folder.

C:\[php folder]

Note: Use semicolon to delimit between System Variables.

php step two

 

3. Open the CMD and enter PATH. Verify your PHP System Variable has been added. (Optional)

php step three

 

4. Type “cd C:\[folder name]” in CMD to enter your PHP directory. Replace [folder name] with the name of your extracted PHP download.

 

5. Type “php -v” to confirm version is correct.

php step five

 

6. Enter “php c:\[directory]\[folder]\[php function].php”. Replace [php function] with your file.

php step six

 

TRY IT OUT:

The example provided is a recursive function that goes through current file directory’s folders and sub-folders, finds images that have dimensions in the name and deletes them from the directory.

1. Download the php iterator example and extract the files.

php try step one

 

2. Repeat step 6 above, replacing folder and function with “php-orionweb\deleteImageSizePhotos.php”

php try step two

 

3. There are 24 pictures, 15 of which are WordPress images sizes containing the image dimensions in the file name. Confirm output of script has the file counts.

php try step three

 

4. Press the up arrow and run the script again. This time you should only 9 files, the other ones have been deleted.

php try step four

 

SUMMARY:

Custom PHP scripts are part of any good developers arsenal, but sometimes building and testing them out can cause issues should you not have access to the web or resources needed. Running PHP from the command line is a great tool for rapid local development that you can help create portable code without a server environment.

By |2018-03-13T08:59:08+00:00April 16th, 2015|Developer Tips|
Get a Quote
close slider

Contact SunAnt