Command line executes the PHP script with parameters and takes parameters
Posted on 2011-07-15
Category: PHP
One, why do we run PHP scripts under the command line?
Personal understanding, there are two main reasons:
1, the use of 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, that is, 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.
Two, command line execution with parameter 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=bbb
Could 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 files, it's easy.
View copy print?
-
- Print_r ($argv);
- Echo "\ n";
- Echo $argc;
- Echo "\ n";
- ?>
2, called under command line
View copy print?
- zhangying@ubuntu:~$ PHP test.php AAA CCC BBBB
- Array
- (
- [0] = = test.php //Parameter 0, the file itself
- [1] = = AAA //Parameter 1
- [2] = = CCC //Parameter 2
- [3] = = BBBB //Parameter 3
- )
- 4 //$ARGC value, total number of parameters
In this way, the root shell script is really like,
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.
The above describes the shell call PHP script, and pass parameters, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.