The shell invokes the PHP script and passes the arguments

Source: Internet
Author: User
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.

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.