php server argv,php – $_SERVER [‘argv’],带有HTTP GET和CLI问题

在PHP中,如果想要在命令行接口(CLI)模式下使用$_SERVER['argc']和$_SERVER['argv'],需要在php.ini中启用register_argc_argv选项。此设置默认关闭,因为出于性能考虑。启用后,可以通过检查php_sapi_name()是否为'cli'来区分脚本运行方式,并分别获取命令行参数或查询字符串参数。在生产环境中,为了性能,通常会禁用此选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手册没有说明这一点,但是,如果你想在CLI模式下运行时要注册$_SERVER [‘argc’],$_SERVER [‘argv’],$argc,$argv,那么需要在php.ini中启用php.ini值

register_argc_argv(默认为关闭[出于性能原因]).

您可以执行以下操作来获取argv或查询字符串args,具体取决于脚本的运行方式:

if (php_sapi_name() == 'cli') {

$args = $_SERVER['argv'];

} else {

parse_str($_SERVER['QUERY_STRING'], $args);

}

以下是php.ini的一些细节:

; This directive determines whether PHP registers $argv & $argc each time it

; runs. $argv contains an array of all the arguments passed to PHP when a script

; is invoked. $argc contains an integer representing the number of arguments

; that were passed when the script was invoked. These arrays are extremely

; useful when running scripts from the command line. When this directive is

; enabled, registering these variables consumes CPU cycles and memory each time

; a script is executed. For performance reasons, this feature should be disabled

; on production servers.

; Note: This directive is hardcoded to On for the CLI SAPI

; Default Value: On

; Development Value: Off

; Production Value: Off

; http://php.net/register-argc-argv

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值