Executing PHP Scripts in the Command Line
Although the PHP scripting language has been mainly developed for the creation of dynamic websites, you can also execute PHP scripts completely independent of the web server. For this purpose, you can call the PHP script on the command line with the so-called PHP Command Line Interpreter (short: PHP CLI).
Requirements:
To run PHP scripts on the command line, you need:
- A Linux-based web hosting package with shell access (SSH).
- A device with an SSH client installed. For computers with Windows operating system we recommend the program Putty.
- Your IONOS FTP server name and the access data for your main FTP user.
To execute PHP files from the command line, do the following:
Note:
The file you want to execute does not require a certain file name extension. For example, your PHP script does not need to end in .php.
Set PHP version
In the example above, you have run your script with the latest version of PHP, PHP 8.1. If your script is not compatible with PHP 8.1, you can also use older versions of PHP.
The following table shows the PHP versions available in IONOS web hosting with PHP CLI. The Path column specifies the command that must be entered on the command line for each version of PHP.
Version | Path |
---|---|
PHP 8.1 | /usr/bin/php8.1-cli |
PHP 8.0 (recommended) | /usr/bin/php8.0-cli |
PHP 7.4 | /usr/bin/php7.4-cli |
PHP 7.3 (deprecated) | /usr/bin/php7.3-cli |
PHP 7.1 (deprecated) | /usr/bin/php7.1-cli |
PHP 5.5 (deprecated) | /usr/bin/php5.5-cli |
PHP 5.4 (deprecated) | /usr/bin/php5.4-cli |
PHP 5.2 (deprecated) | /usr/bin/php5.2-cli |
PHP 4.4 (deprecated) | /usr/bin/php4.4-cli |
Note:
Use outdated PHP versions only if an update of the script is not possible and therefore the use of the older PHP version is necessary for compatibility reasons.
Examples:
- To execute the file my_script.php with PHP 8.0, enter the following command: /usr/bin/php8.0-cli my_script.php
- To run the file my_script.php with PHP 7.4, enter the following command:
/usr/bin/php7.4-cli my_script.php
How to execute PHP code using command line ?
PHP Installation for Windows Users: Follow the steps to install PHP on the Windows operating system.
- Step 1: First, we have to download PHP from it’s official website. We have to download the .zip file from the respective section depending upon on our system architecture(x86 or x64).
- Step 2: Extract the .zip file to your preferred location. It is recommended to choose the Boot Drive(C Drive) inside a folder named php (ie. C:\php).
- Step 3: Now we have to add the folder (C:\php) to the Environment Variable Path so that it becomes accessible from the command line. To do so, we have to right click on My Computer or This PC icon, then Choose Properties from the context menu. Then click the Advanced system settings link, and then click Environment Variables. In the section System Variables, we have to find the PATH environment variable and then select and Edit it. If the PATH environment variable does not exist, we have to click New. In the Edit System Variable (or New System Variable) window, we have to specify the value of the PATH environment variable (C:\php or the location of our extracted php files). After that, we have to click OK and close all remaining windows by clicking OK.
PHP Installation for Linux Users:
apt-get install php5-common libapache2-mod-php5 php5-cli
PHP Installation for Mac Users:
- Mac users can install php using the following command.
curl -s https://php-osx.liip.ch/install.sh | bash -s 7.3
After installation of PHP, we are ready to run PHP code through command line. You just follow the steps to run PHP program using command line.
- Open terminal or command line window.
- Goto the specified folder or directory where php files are present.
- Then we can run php code using the following command:
We can also start server for testing the php code using the command line by the following command:
php -S localhost:port -t your_folder/
Note: While using the PHP built-in server, the name of the PHP file inside the root folder must be index.php, and all other PHP files can be hyperlinked through the main index page.
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
How to Use and Execute PHP Codes in Linux Command Line – Part 1
PHP is an open source server side scripting Language which originally stood for ‘Personal Home Page‘ now stands for ‘PHP: Hypertext Preprocessor‘, which is a recursive acronym. It is a cross platform scripting language which is highly influenced by C, C++ and Java.
A PHP Syntax is very similar to Syntax in C, Java and Perl Programming Language with a few PHP-specific feature. PHP is used by some 260 Million websites, as of now. The current stable release is PHP Version 5.6.10.
PHP is HTML embedded script which facilitates developers to write dynamically generated pages quickly. PHP is primarily used on Server-side (and JavaScript on Client Side) to generate dynamic web pages over HTTP, however you will be surprised to know that you can execute a PHP in a Linux Terminal without the need of a web browser.
This article aims at throwing light on the command-line aspect of PHP scripting Language.
1. After PHP and Apache2 installation, we need to install PHP command Line Interpreter.
# apt-get install php5-cli [Debian and alike System) # yum install php-cli [CentOS and alike System)
Next thing, we do is to test a php (if installed correctly or not) commonly as by creating a file infophp.php at location ‘/var/www/html‘ (Apache2 working directory in most of the distros), with the content , simply by running the below command.
# echo '' > /var/www/html/infophp.php
and then point your browser to http://127.0.0.1/infophp.php which opens this file in web browser.
Same results can be obtained from the Linux terminal without the need of any browser. Run the PHP file located at ‘/var/www/html/infophp.php‘ in Linux Command Line as:
# php -f /var/www/html/infophp.php
Since the output is too big we can pipeline the above output with ‘less‘ command to get one screen output at a time, simply as:
# php -f /var/www/html/infophp.php | less
Here Option ‘-f‘ parse and execute the file that follows the command.
2. We can use phpinfo() which is a very valuable debugging tool directly on the Linux command-line without the need of calling it from a file, simply as:
Here the option ‘-r‘ run the PHP Code in the Linux Terminal directly without tags < and >.
3. Run PHP in Interactive mode and do some mathematics. Here option ‘-a‘ is for running PHP in Interactive Mode.
# php -a Interactive shell php > echo 2+3; 5 php > echo 9-6; 3 php > echo 5*4; 20 php > echo 12/3; 4 php > echo 12/5; 2.4 php > echo 2+3-1; 4 php > echo 2+3-1*3; 2 php > exit
Press ‘exit‘ or ‘ctrl+c‘ to close PHP interactive mode.
4. You can run a PHP script simply as, if it is a shell script. First Create a PHP sample script in your current working directory.
# echo -e '#!/usr/bin/php\n' > phpscript.php
Notice we used #!/usr/bin/php in the first line of this PHP script as we use to do in shell script (/bin/bash). The first line #!/usr/bin/php tells the Linux Command-Line to parse this script file to PHP Interpreter.
Second make it executable as:
5. You will be surprised to know you can create simple functions all by yourself using the interactive shell. Here is the step-by step instruction.
Start PHP interactive mode.
Create a function and name it addition. Also declare two variables $a and $b.
php > function addition ($a, $b)
Use curly braces to define rules in between them for this function.
Define Rule(s). Here the rule say to add the two variables.
All rules defined. Enclose rules by closing curly braces.
Test function and add digits 4 and 3 simply as :
php > var_dump (addition(4,3));
Sample Output
You may run the below code to execute the function, as many times as you want with different values. Replace a and b with values of yours.
php > var_dump (addition(a,b));
php > var_dump (addition(9,3.3));
Sample Output
You may run this function till you quit interactive mode (Ctrl+z). Also you would have noticed that in the above output the data type returned is NULL. This can be fixed by asking php interactive shell to return in place of echo.
Simply replace the ‘echo‘ statement in the above function with ‘return‘
and rest of the things and principles remain same.
Here is an Example, which returns appropriate data-type in the output.
Always Remember, user defined functions are not saved in history from shell session to shell session, hence once you exit the interactive shell, it is lost.
Hope you liked this session. Keep Connected for more such posts. Stay Tuned and Healthy. Provide us with your valuable feedback in the comments. Like ans share us and help us get spread.