Introduction to PHP command line script receiving parameters

Source: Internet
Author: User
The following small series for everyone to bring a PHP get '/' parameter value simple method. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

Through the output $globals can see the "/" parameters are present in $_server[' Path_info '];

Declare an array to get the arguments we passed after '/'

$arr = explode ('/', $_server[' path_info ');//print_r ($arr) View details

PHP is usually made HTTP request, you can use Get or post method to receive parameters, and sometimes need to be executed under the shell command PHP as a script, such as scheduled tasks. This involves the question of how to send a reference to PHP under a shell command, usually in three ways.
One, using $ARGV or $ARGC parameter receive

<?php/** * Use $ARGC $argv accept parameter */echo "received {$ARGC} parameters";p Rint_r ($ARGV);

Perform

[root@dell113 lee]#/usr/local/php/bin/php test.php

1 Parameters received

Array (    [0] = test.php) [root@dell113 lee]#/usr/local/php/bin/php test.php a b C D received 5 parameters array (    [0] + = Test . PHP    [1] = a    [2] = b    [3] = = C    [4] + D) [root@dell113 lee]#

Second, using the Getopt function

<?php/** * Use the getopt function */$param _arr = getopt (' a:b: ');p Rint_r ($param _arr);

Perform

[root@dell113 lee]#/usr/local/php/bin/php test.php-a 345Array (    [A] = 345) [root@dell113 lee]#/usr/local/php/ bin/php test.php-a 345-b 12q3array (    [A] = 345    [b] = 12q3) [root@dell113 lee]#/usr/local/php/bin/php Test . php-a 345-b 12q3-e 3322ffArray (    [A] = 345    [b] = 12q3)

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.