PHP is mainly used to develop web applications, but it can also be used for other purposes. One of the useful features of PHP is the support of SAPI (Server Application Programming Interface) type named CLI (Command Line Interface). The CLI SAPI is released in PHP 4.2.0 version for the first time. The –enable-cli option is used
to enable this feature, and this option is enabled in the new version of PHP by default. Furthermore, the –disable-cli option is used to disable this feature. Different CLI options are used in PHP, and the way of executing PHP script from the command line is described in this tutorial. Some mostly used CLI options. They are explained below: You have to install PHP on your operating system to check the CLI options of PHP. No web server is required to run the PHP script from the terminal. So, you can run the PHP command from any location, and the PHP file can be stored in any location. The uses of different CLI options are shown in this part of this tutorial. Example-1: Check the version of CLI using –v Run PHP command with -v option from the
terminal. The following output shows CLI version 7.4.3 installed on the system. Example-2: Display the output of phpinfo() using -i Run PHP command with -i option from the terminal. The following output shows the detailed information returned by the phpinfo() function. Example-3: Execute a simple PHP script without PHP delimiter using -r Run PHP command with -r option and a script from the terminal. $ php -r 'echo "Welcome to Linux Hint\n";' The following output will appear after running the script. The string value is printed with a newline here. Example-4: Execute PHP script from a file using -f Create a PHP file named cli1.php with the following script. Here, STDIN is defined at the beginning of the script to take the input from the user. Next, two string values will be taken from the user where the input value can be a maximum of 5 characters. Then, the values will be converted into integer values, and their sum will be stored in a variable that will be printed later. #!/usr/bin/php -q //Take two numeric values as input //Convert the string data to number and calculate sum //Print the result of the summation Run the PHP file from the terminal using the PHP command with -f option. You have to mention the path of the PHP file properly in the command. $ php -f /var/www/html/php/cli1.php In the following output, 30 and 70 are taken as input, and 100 is printed as output. Example-5: Check the syntax of PHP file using -l Create a PHP file named cli2.php with the following script. Here, STDIN is defined at the beginning of the script to take the input from the user. Next, a string value will be taken from the user and that is printed after formating. #!/usr/bin/php
-q echo "What is your favorite color?\n"; Run the above script with the -l option to check the syntax error. If the script contains any syntax error, then the output will display the error with a line number. Otherwise, it will print the value ‘No syntax error detected’. It is better to check the script, whether contains any syntax error or not, before executing the script. $ php -l /var/www/html/php/cli2.php The following output shows that the script has no syntax error. For example, if any semicolon(;) is omitted after any line, then it will display the error with line number. Example-6: Display PHP script from a file by omitting comments and whitespaces using -w You can check the use of the -w option by creating any PHP script file with comments and whitespaces. Create a PHP file named cli3.php with the following code that contains two comments and many whitespaces. The output will show the full script by removing comments and whitespaces. #!/usr/bin/php -q //Assign a numeric value //Check the number is less than 100 or not ?> Run the above script with -w option using PHP command. $ php -w /var/www/html/php/cli3.php The following output will appear after running the script. ConclusionYou can test the PHP script without using any web server by using the CLI feature. Many other options exist for PHP CLI for different purposes. You can get the list of all CLI options by running the PHP command with the -h option if you want to know more about the PHP CLI. The most commonly used CLI options are explained in this tutorial, with examples, to let the readers know more about this PHP feature. About the authorI am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help. |