A method for executing a PHP script with parameters based on the command line and getting the parameters, PHP script
This paper analyzes the method of executing PHP script with parameters based on command line and obtaining parameters. Share to everyone for your reference, as follows:
Why do we run PHP scripts under the command line?
Personal understanding, there are two main reasons:
1. Use crontab to run PHP, can give the server decompression, of course, there is a condition here, is the real-time requirements are not high. For example: SNS in the friend dynamic, this real-time requirement is not high, but the amount of data is large, this time run regularly, will give the Web server, the database server share a lot of pressure.
2. Is that we have to do something regularly, such as: I want to delete one months ago, user message, this time, write PHP script in crontab to execute, run once a day on the line. Instead of manually executing the PHP program.
Second, command line execution with parameters PHP, and get parameters
One thing is very important, is the command line execution PHP, is not going to Apache and other such things, there is no HTTP protocol, all get,post parameters do not work at all, and will also error , as follows:
zhangying@ubuntu:~$ PHP test.php?aaa=bbbcould not open input file:test.php?aaa=bbb
Normally, there is no need to pass parameters to a timed php script, but sometimes it is .
1. test.php test file, it's easy.
<?phpprint_r ($ARGV); echo "\ n"; echo $argc; echo "\ n";? >
2. Call under command line
zhangying@ubuntu:~$ PHP test.php AAA CCC Bbbbarray ([0] = test.php //Parameter 0, file itself [1] = + AAA //Parameter 1 [2] + = CC C //Parameter 2 [3] = bbbb //Parameter 3) 4//$ARGC value, total number of parameters
In this way, the root shell script is really like
Copy the Code code as follows: zhangying@ubuntu:~$ SH c1.sh aaa BBB
I passed two parameters to c1.sh. AAA Bbb,shell will get three parameters, the file itself, $2 for parameter 1,$2. The difference is that PHP gets the situation as an array, while the shell is not.
More readers interested in PHP related content can view this site topic: "PHP Basic Grammar Primer Tutorial" and "PHP Object-oriented Programming primer"
I hope this article is helpful to you in PHP programming.
Articles you may be interested in:
- PHP command line script three ways to receive incoming parameters
- Instructions for using the PHP command line and the command-line arguments
- PHP command line parameters and application
- To run a PHP script [with parameters] at the command line
http://www.bkjia.com/PHPjc/1095685.html www.bkjia.com true http://www.bkjia.com/PHPjc/1095685.html techarticle A method for executing a PHP script with parameters based on the command line and getting the parameters, PHP script This article analyzes the method of executing the PHP script with parameters based on the command line and obtaining the parameters. Share for the big ...