This paper analyzes the method of executing the PHP script with parameters and obtaining the parameters based on the command line. Share to everyone for your reference, specific as follows:
Why do we run PHP scripts at the command line?
Personal understanding, there are two main reasons:
1. Use crontab to run PHP, you can reduce the pressure on the server, of course, here is a condition, is not high real-time requirements. 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 the crontab to execute, run once a day on the line. Instead of manually executing the PHP program.
Second, the command line executes with the parameter PHP, and obtains 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
Normally, you don't need to pass parameters to a timed php script, but sometimes you do.
1. test.php test file, it's easy.
<?php
Print_r ($ARGV);
echo "\ n";
echo $ARGC;
echo "\ n";
? >
2. Call under command line
zhangying@ubuntu:~$ PHP test.php AAA CCC bbbb
Array
(
[0] => test.php //Parameter 0, file itself
[1] => aaa< c6/>//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 really looks like
Copy Code code as follows:
zhangying@ubuntu:~$ sh c1.sh Triple A BBB
I passed two parameters to c1.sh. 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.
For more information on PHP-related content readers can view the site topics: "Introduction to PHP Basic grammar" and "PHP object-oriented programming Introduction to the course"
I hope this article will help you with the PHP program design.