How to Run PHP File From Command Line
More often than not, we run PHP over web servers, to be precise. After all, PHP is a server-side language.
However, it is also a scripting language that you can run over a shell or command line. With it, you can run PHP interactively as you would Python or your typical Git or Bash commands.
This tutorial will teach you how to run PHP from the command line easily and with a few commands.
PHP Installation
Before you can run PHP on your local PC, you need to have PHP installed. To do that, you will go to PHP’s website for Windows users, this article for Mac users, or this installation guide for Linux users.
For Windows users, you can extract the downloaded file, copy it to C:\php
, add it to the Environment Variable path, and check for the PHP version to ascertain complete configuration using the command below.
php -v
Once you see a version, you can start running PHP via your command line. Also, you can run your PHP on your servers or over the cloud.
Run PHP Interactively
php -a
The output of this command within the shell or CLI is below.
> php -a
Interactive mode enabled
Then, you can execute the PHP statement just like below.
php > echo "This is the PHP interpreter";
This is the PHP interpreter
Parse PHP File
To parse an existing PHP file with your code, you need to be in the working directory of the PHP file. The pwd
command is useful to check which working directory you are in.
pwd
On Windows, it should give you something like the below.
Path
----
cd C:\Users\HP\Documents
You should see something like this on Unix (macOS) and Linux.
/home/runner/ProudScientificMemorypool
If you are not in the right directory, you can navigate to the right directory using the cd
command.
cd C:\Users\HP\Documents
You can read more about navigating the command line interface on Windows, MacOS and Linux.
Once you are sure you are in the right directory of your PHP file, you can parse (run) the PHP file via the following command.
php -f main.php
Or you can use:
php main.php
You can export the code results from your PHP file into a txt
or html
via the command below.
php -f main.php > results.html
However, suppose the PHP code does have a readline()
function as the code from the tutorial on how to calculate the average of a continuous number set in PHP. In that case, you could experience strange behavior, such as not seeing the prompt.
Therefore, it is important you know what PHP code you are parsing. The output, results.html
, looks like the below within an HTML file.
Adding numbers repeatedly to get the average at all the intervals
If you want to terminate the program, type 000
Current average is 123
Current average is 178.5
Current average is 234
Current average is 289.5
Current average is 345
Current average is 400.5
Current average is 456
Current average is 510.25
Current average is 553.66666666667
Average of all the numbers (9) is 553.66666666667.
Run a Single Line of Code
To run only a small part/one line of code, you can use:
php -r '$statement = "I know PHP"; echo "$statement\n";'
Output:
I know PHP
Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.
LinkedIn