PHP is principally used to develop net functions, but it surely can be used for different functions. One of many helpful options of PHP is the help of SAPI (Server Utility Programming Interface) sort named CLI (Command Line Interface). The CLI SAPI is launched in PHP 4.2.zero model for the primary time. The –enable-cli choice is used to allow this function, and this feature is enabled within the new model of PHP by default. Moreover, the –disable-cli choice is used to disable this function.
Completely different CLI choices are utilized in PHP, and the best way of executing PHP script from the command line is described on this tutorial.
Some largely used CLI choices. They’re defined beneath:
|-r||It’s used to execute PHP script with out utilizing PHP delimiter ().|
|-f||It’s used to execute the PHP file.|
|-i||It’s used to show the output of phpinfo().|
|-l||It’s used to test the syntax of the given PHP file.|
|-w||It’s used strip feedback and whitespaces from the given file.|
|-a||It’s used to run in an interactive shell setting.|
|-h||It’s used to show all out there choices with an evidence of CLI.|
|-v||It’s used to show the PHP CLI model info.|
Makes use of of CLI choices:
It’s a must to set up PHP in your working system to test the CLI choices of PHP. No net server is required to run the PHP script from the terminal. So, you may run the PHP command from any location, and the PHP file may be saved in any location.
The makes use of of various CLI choices are proven on this a part of this tutorial.
Instance-1: Test the model of CLI utilizing –v
Run PHP command with -v choice from the terminal.
The next output reveals CLI model 7.4.Three put in on the system.
Instance-2: Show the output of phpinfo() utilizing -i
Run PHP command with -i choice from the terminal.
The next output reveals the detailed info returned by the phpinfo() operate.
Instance-3: Execute a easy PHP script with out PHP delimiter utilizing -r
Run PHP command with -r choice and a script from the terminal.
$ php -r ‘echo “Welcome to Linux Hintn”;’
The next output will seem after operating the script. The string worth is printed with a newline right here.
Instance-4: Execute PHP script from a file utilizing -f
Create a PHP file named cli1.php with the next script. Right here, STDIN is outlined at the start of the script to take the enter from the person. Subsequent, two string values will likely be taken from the person the place the enter worth could be a most of 5 characters. Then, the values will likely be transformed into integer values, and their sum will likely be saved in a variable that will likely be printed later.
// Outline STDIN to learn knowledge from PHP
//Take two numeric values as enter
echo “Enter the worth of a: “;
$number1 = fread(STDIN,5);
echo “Enter the worth of b: “;
$number2 = fread(STDIN,5);
//Convert the string knowledge to quantity and calculate sum
$sum = (int)$number1 + (int)$number2;
//Print the results of the summation
printf(“The sum of %d and %d is %dn”,$number1, $number2, $sum);
Run the PHP file from the terminal utilizing the PHP command with -f choice. It’s a must to point out the trail of the PHP file correctly within the command.
$ php -f /var/www/html/php/cli1.php
Within the following output, 30 and 70 are taken as enter, and 100 is printed as output.
Instance-5: Test the syntax of PHP file utilizing -l
Create a PHP file named cli2.php with the next script. Right here, STDIN is outlined at the start of the script to take the enter from the person. Subsequent, a string worth will likely be taken from the person and that’s printed after formating.
// Outline STDIN to learn knowledge from PHP
echo “What’s your favourite shade?n”;
//Take enter from the person
$shade = fread(STDIN,10);
//Print the enter worth
printf(“Your chosen shade is: %sn”,$shade);
Run the above script with the -l choice to test the syntax error. If the script incorporates any syntax error, then the output will show the error with a line quantity. In any other case, it can print the worth ‘No syntax error detected’. It’s higher to test the script, whether or not incorporates any syntax error or not, earlier than executing the script.
$ php -l /var/www/html/php/cli2.php
The next output reveals that the script has no syntax error. For instance, if any semicolon(;) is omitted after any line, then it can show the error with line quantity.
Instance-6: Show PHP script from a file by omitting feedback and whitespaces utilizing -w
You possibly can test using the -w choice by creating any PHP script file with feedback and whitespaces. Create a PHP file named cli3.php with the next code that incorporates two feedback and lots of whitespaces. The output will present the complete script by eradicating feedback and whitespaces.
$num = 78;
//Test the quantity is lower than 100 or not
if($num < 100)
echo “The worth $num is lower than 100n”;
Run the above script with -w choice utilizing PHP command.
$ php -w /var/www/html/php/cli3.php
The next output will seem after operating the script.
You possibly can take a look at the PHP script with out utilizing any net server by utilizing the CLI function. Many different choices exist for PHP CLI for various functions. You will get the record of all CLI choices by operating the PHP command with the -h choice if you wish to know extra in regards to the PHP CLI. Essentially the most generally used CLI choices are defined on this tutorial, with examples, to let the readers know extra about this PHP function.
php not connecting to mysql ubuntu,php mysql connection problem linux,php 5.4 connect to mysql,remote mysql connection php,mysql_connect in mariadb,how to login to mariadb from command line,how to run php files in linux browser,linux running php scripts,bash call php script with parameters,run php in terminal windows,run php script in unix,execute php script from command line linux,linux commands cheat sheet 2020 pdf,red hat linux cheat sheet,cheatography linux,kali linux cheat sheet pdf,linux cheat sheet wallpaper,command line cheat sheet pdf,rpm -qa command use,rpm vs yum,rpm list installed packages by date,rpm linux command,update using rpm command,rpm – qa grep,how to connect mysql database with php in linux,php linux,how to create a website using php and mysql in xampp,php-mariadb,php mysql program examples,how to save data from html form to a database using php