Execute a PHP Script using a Command Line

Scripting Computer Language Code Programming Developer Technology Concept

As a beginner, you will be surprised to know the hidden hacks of PHP. As we say, you don’t need a web server to run PHP scripts. But how come it is possible? You can use the PHP CLI (PHP Command Line Interface) to execute them from the command line.

This is a handy way to test your PHP code or run simple tasks without setting up a web server. This write-up explains how to run PHP scripts using the command line and how to install the PHP CLI on Windows, Linux, and MacOS. So, let’s get started!

Step 1: Install PHP on your System

The first step starts with installing the PHP on your device. Below is a step-by-step process for installing PHP on Windows, Linux, and MacOS.

PHP Installation for Windows Users

Here’s a complete step-wide procedure for downloading and installing the PHP on your device:

Step 1: To install PHP on your system, you need to download the .zip file from the official website of PHP. Make sure you choose the right file for your system architecture (x86 or x64). You can find the download links in the [PHP Downloads] section.

Step 2: You need to extract the .zip file that you downloaded from the PHP website. The best place to put it is in a folder called php on your Boot Drive (C Drive). That way, you can easily access it later. For example, you can use C:\php as the folder name.

Step 3: To make the folder (C:\php) accessible from the command line, we need to add it to the Environment Variable Path. Right-click on the icon of My Computer or This PC and choose Properties from the menu that appears.

Step 4: Click on the link that says Advanced system settings and then click on Environment Variables.

Step 5: Look for the System Variables section and find the PATH environment variable. Select it and click Edit. If there is no PATH environment variable, click New instead.

Step 6: In the window that opens, enter the value of the PATH environment variable as C:\php or the location where you extracted the PHP files. Then click OK and close all the other windows by clicking OK.

PHP Installation for Linux OS

If you are a Linux user, you can follow these steps to install PHP on your system:

apt-get install php5-common libapache2-mod-php5 php5-cli

Optionally, you can also install other PHP-related packages that you may need, such as php-cli, php-cgi, php-mysql, or php-pgsql.

PHP Installation for MacOS

If you are a Mac user, you can follow these steps to install PHP on your system:

curl -s https://php-osx.liip.ch/install.sh | bash -s 7.3

After you successfully install PHP on your system, you are all set to execute the PHP script using the command line. The next section of this blog will explain the process of executing PHP scripts using the command line.

Step2: Execute PHP Scripts using Comand Line

To execute PHP scripts from the terminal, you need to have PHP installed on your system and make sure that the PHP executable is in your PATH environment variable. Then you can follow these steps:

Before you run any PHP code from the command line, you need to make sure that PHP is installed on your system. You can verify the PHP installation and version by typing this command in the terminal:

php -v

To write PHP code, you need a text editor and a .php file extension. For example, you can make a file named hello.php with this code inside:

<?php echo "Hello, world!"; ?>

Open your terminal and go to the folder where you stored your PHP file. You can change directories by using the cd command.

To execute your PHP file, you need to type php filename.php in the terminal. For instance, you can type php hello.php to run hello.php. You will see the output of your code in the terminal.

Step 3: Test the Code

You can also test your PHP code by starting a server from the command line. To do that, you need to use this command:

php -S localhost:port -t your_folder/

For example, you can start a server on port 8000 by typing php -S localhost:8000 -t .. Then you can access your PHP files from your browser by using the command:

http://localhost:8000/filename.php.

Conclusion 

Through this tutorial, we have tried to explain how to execute PHP scripts using the command line. You have seen how to install PHP on your system, write PHP code in a text editor, run PHP code in the terminal, pass arguments to PHP scripts, and start a server for testing PHP code.

By following these steps, you can easily run and test your PHP code from the command line and develop your own web applications using PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *