How to execute a php script with parameters and obtain parameters based on command lines

Source: Internet
Author: User
: This article describes how to execute a php script with parameters based on the command line and obtain parameters. if you are interested in the PHP Tutorial, refer to it. This article analyzes how to execute a php script with parameters based on command lines and obtain parameters. 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

It is very important to execute php in the command line. it does not take apache and other such things. there is no http protocol, and all get and post parameters do not work at all, and an error is reported, as follows:

zhangying@ubuntu:~$ php test.php?aaa=bbbCould not open input file: test.php?aaa=bbb

In general, you do not need to pass parameters to the php script that runs regularly, but sometimes it is required.

1. test. php test file. it's easy.

<?phpprint_r($argv);echo "\n";echo $argc;echo "\n";?>

2. Call

Zhangying @ ubuntu :~ $ Php test. php aaa ccc bbbbArray ([0] => test. php // parameter 0, file [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

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.