the command line executes the PHP script with parameters and gets the parameters
Published in 2011-07-15
Category directory: PHP
One, why do we run PHP scripts at the command line?
Personal understanding, there are two main reasons:
1, using crontab to run PHP, you can reduce the pressure on the server, of course, there is a condition, that is, real-time requirements are not high. For example: SNS in the friend dynamic, this real-time request is not high, but the data quantity is big, this time runs regularly, will give the Web server, the database server share the pressure.
2, is that we have to regularly to complete a certain thing, such as: I want to delete one months ago, the 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, the command line executes with parameter PHP, and gets the parameter
One thing is very important, is that the command line to execute PHP, is not to take Apache and other such things, there is no HTTP protocol, all get,post pass parameters do not work, and will also error, as follows:
zhangying@ubuntu:~$ php test.php?aaa=bbb
could not open input file:test.php?aaa=bbb
a case, do not need to run to the regular PHP script pass parameters, but sometimes, is required.
1,test.php test file, it's easy. View copy print? <?php Print_r ($ARGV); echo "\ n"; Echo $ARGC; echo "\ n"; ?>
2, command line down with view copy print? zhangying@ubuntu:~$ PHP test.php AAA CCC bbbb Array ([0] => test.php//Parameter 0, file itself [1] => AAA// Parameter 1 [2] => CCC//Parameter 2 [3] => BBBB//Parameter 3) 4//$ARGC value, total number of parameters
this way of passing the argument, the root shell script is really like,
zhangying@ubuntu:~$ sh c1.sh triple A BBB
I passed the c1.sh two parameters. AAA Bbb,shell will get three parameters, $ $ for the file itself, and 1,$2 for parameter 2. The difference is that PHP gets an array of the situation, while the shell is not.