Name PHP specifiers in printf() strings

后端 未结 4 2159
情书的邮戳
情书的邮戳 2021-02-19 18:47

Is there a way in PHP to name my specifiers like in Python?

I want this in PHP:

$foo = array(\'name\' => 24);
printf(\"%(name)d\", $foo);
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 19:02

    You can do it with sprintf by numbering the parameters, like so:

    echo sprintf('hello, %2$s. What is your %1$s', 'age', 'Jesse');
    


    Outputs the following string:

    hello, Jesse. What is your age

提交回复
热议问题