Introduction to php command line script receiving passed parameters

零下一度
Release: 2023-03-14 08:06:02
Original
2331 people have browsed it

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)查看详细信息
Copy after login

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);
Copy after login

Execute

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

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]#
Copy after login

2. Use getopt Function

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

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template