For
The first method is to use php-f/path/to/yourfile. php. Call the php cli interpreter and pass parameters to the script. In this method, you must first set the path of the php interpreter. Before Running CLI on Windows platform, you must set a command similar to path c: php, which also loses the meaning of the first line of the CLI script, therefore, this method is not recommended.
The second method is to first run chmod + x <script file name to be run> (UNIX/Linux environment) and set the PHP file to executable permissions, then add the declaration in the first line of the CLI script header (similar #! /Usr/bin/php or php cli interpreter location), and then directly execute the command line. This is the preferred method for CLI. We recommend that you use this method.
Let's take a look at how to compile the php cli script.
Write a CLI script
First, create a php script named myfile. PHP to run the php cli script. This script is very simple and only displays "Hello php cli !". The script code is as follows:
- #! /Usr/local/bin/php-q
- <? Php
- // On Windows, the uplink should be:
#! C: phpphp.exe-q
- Echo "Hello php cli! ";
- ?>
Do not forget to set the executable permission for this file:
$ Chmod 755 myfile. php
Run the following command and press Enter:
$./Myfile. php
If you want to run this script in Windows, you can directly run the php cli script without setting file attributes.
Again: on the Windows platform, the first line of the client script must write the location of php.exe, as shown in the following figure. (In addition, if you want to add a comment statement to the CLI script, you must write the comment in the PHP tag, because CLI interpretation only recognizes the first line, and does not consider it as a syntax error in the PHP tag ):
#! C: phpphp.exe-q
In this way, you can see that the information in the command line has been printed to prove that the php cli script has been successfully run.