PHP CLI - 逗号分隔的 key/value 对用大括号括起来 - 它们是什么?

PHP CLI - comma-separated key/value pairs wrapped in curly braces - what are they?

运行 PHP 来自 cli 的脚本为:

$ php test.php {a=1,b=2,c=3}

结果 $argv 结构为:

[
    0 => test.php
    1 => a=1
    2 => b=2
    3 => c=3
]

问题。

  1. 在这种情况下,{ ... } 的预期含义是什么?
  2. 它是 PHP 的东西还是 bash 的东西?
  3. 是否有描述此行为的联机文档? (是的,我已经用谷歌搜索了 - 但没有成功)

谢谢!

根据 section 3.5.1 of the Bash manual:

Brace expansion is a mechanism by which arbitrary strings may be generated. This mechanism is similar to filename expansion, but the filenames generated need not exist. Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a sequence expression between a pair of braces, followed by an optional postscript. The preamble is prefixed to each string contained within the braces, and the postscript is then appended to each resulting string, expanding left to right.

在您的情况下,没有前言或后记,因此它只是扩展到列表中的每个元素。

使用echo可以看到结果:

echo {a=1,b=2,c=3}

输出:

a=1 b=2 c=3

如果您要使用 preamblepostscript:

echo before{a=1,b=2,c=3}after

你得到:

beforea=1after beforeb=2after beforec=3after

我通常在尝试将文件复制或移动到备份时使用它:

cp somefile.txt{,.bak}

扩展为:

cp somefile.txt somefile.txt.bak

{}PHP 中表示为对象,当 var_dump 是 class 的实例时,您会发现它被 {} 包裹。我发现有一份文档描述了如何使用 $argv https://www.php.net/manual/en/reserved.variables.argv.php