This article mainly introduces how to execute a php script with parameters based on the command line and obtain parameters. it analyzes the principles, usage, and precautions of running PHP code using the command line, for more information about how to execute a php script with parameters based on command lines and obtain parameters, see this document. We will share this with you for your reference. The details are as follows:
1. why should we run the php script on the command line?
For personal understanding, there are two main reasons:
1. using crontab to run php can decompress the server. of course, there is a condition that the real-time requirement is not high. For example, the real-time requirement for friend dynamics in sns is not high, but the data volume is large. if you run regularly at this time, the web server and database server will share a lot of pressure.
2. that is, we need to regularly complete a task. for example, I want to delete a message from the user a month ago. at this time, the php script written is executed on crontab, you just need to run it once a day. Instead of executing the php program manually.
2. execute php with the parameter in the command line and obtain the parameter
One thing is very important,The code is as follows:
Zhangying @ ubuntu :~ $ Sh c1.sh aaa bbb
I passed two aaa bbb parameters to c1.sh, and shell will get three parameters. $0 is the file itself, $1 is parameter 1, and $2 is parameter 2. The difference is that php gets an array instead of a shell.