Home >Backend Development >PHP Tutorial >Introduction to php command line script receiving passed parameters

Introduction to php command line script receiving passed parameters

零下一度
零下一度Original
2017-07-16 16:50:352367browse

The editor below will bring you an articlephpA simple method to obtain the value of the '/' passed parameter. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let's follow the editor and take a look.

By outputting $GLOBALS, you can see that the parameters after '/' exist in $_SERVER['PATH_INFO'];

Declare one ArrayTo get the parameters we passed after '/'

$arr = explode('/', $_SERVER['PATH_INFO']);

//print_r($arr)查看详细信息

Usually PHP makes http requests. You can use GET or POST to receive parameters. Sometimes you need to Under the shell command, PHP is executed as a script, such as a scheduled task. This involves the issue of how to pass parameters to php under the shell command. There are usually three ways to pass parameters.
1. Use $argv or $argc parameter to receive

<?php
/**
 * 使用 $argc $argv 接受参数
 */
 
echo "接收到{$argc}个参数";
print_r($argv);

Execute

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

Receive 1 parameter

Array(    [0] => test.php)[root@DELL113 lee]# /usr/local/php/bin/php test.php a b c d接收到5个参数Array(    [0] => test.php    [1] => a    [2] => b    [3] => c    [4] => d)[root@DELL113 lee]#

2. Use getopt Function

<?php/** * 使用 getopt函数 */ $param_arr = getopt(&#39;a:b:&#39;);print_r($param_arr);

Execution

[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 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)

The above is the detailed content of Introduction to php command line script receiving passed parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn