Name PHP specifiers in printf() strings

后端 未结 4 2178
情书的邮戳
情书的邮戳 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:15

    Use strtr:

    $foo = array('%name%' => 24);
    strtr("%name%", $foo); // 24
    

    You could also do this:

    "${foo['name']}"
    

    Or this:

    $name = 24;
    "$name"
    

提交回复
热议问题