The shell invokes the PHP script and passes the parameters

Source: Internet
Author: User
Command line executes the PHP script with parameters and takes parameters

Posted on 2011-07-15

Category: PHP

One, why do we run PHP scripts under the command line?

Personal understanding, there are two main reasons:

1, the use of crontab to run PHP, can give the server decompression, of course, there is a condition here, is the real-time requirements are not high. For example: SNS in the friend dynamic, this real-time requirement is not high, but the amount of data is large, this time run regularly, will give the Web server, the database server share a lot of pressure.

2, that is, we have to do something regularly, such as: I want to delete one months ago, 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, command line execution with parameter PHP, and get parameters

One thing is very important, is the command line execution PHP, is not going to Apache and other such things, there is no HTTP protocol, all get,post parameters do not work at all, and will also error, as follows:

zhangying@ubuntu:~$ PHP test.php?aaa=bbb

Could not open input file:test.php?aaa=bbb

Normally, there is no need to pass parameters to a timed php script, but sometimes it is.

1,test.php test files, it's easy.

View copy print?

    1. Print_r ($argv);
    2. Echo "\ n";
    3. Echo $argc;
    4. Echo "\ n";
    5. ?>

2, called under command line

View copy print?

    1. zhangying@ubuntu:~$ PHP test.php AAA CCC BBBB
    2. Array
    3. (
    4. [0] = = test.php //Parameter 0, the file itself
    5. [1] = = AAA //Parameter 1
    6. [2] = = CCC //Parameter 2
    7. [3] = = BBBB //Parameter 3
    8. )
    9. 4 //$ARGC value, total number of parameters

In this way, the root shell script is really like,

zhangying@ubuntu:~$ SH c1.sh aaa BBB

I passed two parameters to c1.sh. AAA Bbb,shell will get three parameters, the file itself, $2 for parameter 1,$2. The difference is that PHP gets the situation as an array, while the shell is not.


The above describes the shell call PHP script, and pass parameters, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.