Methods for executing PHP scripts with parameters and obtaining parameters based on command line _php tips

Source: Internet
Author: User
Tags php script

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.

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.