PHP command line script receives incoming parameters in three ways _php instance

Source: Internet
Author: User
Tags stdin

PHP is usually requested by HTTP, you can use Get or post methods to receive parameters, and sometimes need to be under the shell command PHP as a script execution, such as timed tasks. This involves the question of how to pass a reference to PHP under a shell command, usually with three different ways to pass the argument.
use $ARGV or $ARGC parameters to receive

Copy Code code as follows:

<?php
/**
* Use $ARGC $argv Accept Parameters
*/

echo "received {$ARGC} parameters";
Print_r ($ARGV);

Perform
Copy Code code as follows:

[root@dell113 lee]#/usr/local/php/bin/php test.php
1 parameter array received
(
[0] => test.php
)
[root@dell113 lee]#/usr/local/php/bin/php test.php a b c D
5 parameter array received
(
[0] => test.php
[1] => a
[2] => b
[3] => C
[4] => D
)
[Root@dell113 lee]#

second, using the Getopt function
Copy Code code as follows:

<?php
/**
* Using the GETOPT function
*/

$param _arr = getopt (' a:b: ');
Print_r ($param _arr);

Perform
Copy Code code as follows:

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

third, prompts the user to enter
Copy Code code as follows:

<?php
/**
* Prompt user input, similar to Python
*/
Fwrite (STDOUT, ' Please enter your blog name: ');
Echo ' The information you entered is: '. fgets (STDIN);

Perform
Copy Code code as follows:

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

Please enter your blog name: Cloud Habitat Community www.jb51.net
The information you have entered is: Yun-Habitat Community www.jb51.net
You can do the same thing, don't let the user enter empty information
Copy Code code as follows:

<?php
/**
* Prompt user input, similar to Python
*/

$fs = true;

do{
OIF ($FS) {
Fwrite (STDOUT, ' Please enter your blog name: ');
$fs = false;
}else{
Fwrite (STDOUT, ' Sorry, blog name can not be empty, please re-enter your blog name: ');
}

$name = Trim (fgets (STDIN));

}while (! $name);

Echo ' The information you entered is: '. $name. ' \ r \ n ";

Perform
Copy Code code as follows:

[root@dell113 lee]#/usr/local/php/bin/php test.php
Please enter your blog name:
Sorry, blog name can not be empty, please re-enter your blog name: Cloud Habitat Community
The information you have entered is: cloud-dwelling community

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.