Since PHP is a server side scripting language, a lot of website 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.
1. First you will need download the latest version of PHP and extract it to your C:\ directory.
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.
Note: Use semicolon to delimit between System Variables.
3. Open the CMD and enter PATH. Verify your PHP System Variable has been added. (Optional)
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.
6. Enter “php c:\[directory]\[folder]\[php function].php”. Replace [php function] with your file.
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.
2. Repeat step 6 above, replacing folder and function with “php-orionweb\deleteImageSizePhotos.php”
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.
4. Press the up arrow and run the script again. This time you should only 9 files, the other ones have been deleted.
Custom PHP scripts are part of any good developer’s 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 web development that you can help create portable code without a server environment.
See also Get Git and You’ve Got the Power