【问题标题】:PHP sprintf() no replacing with argument swappingPHP sprintf() 不能用参数交换代替
【发布时间】:2013-02-04 18:48:44
【问题描述】:

以下代码生成未定义变量 $s 而不是“二号”

define("T1","one");
define("T2","two");

$test="number %2$s";

$test=sprintf($test, T1,T2);

echo $test;

【问题讨论】:

  • 用 \$ 转义 $ 或使用单个配额

标签: php


【解决方案1】:

单引号解决您的问题。双引号导致 PHP 将您的 '$' 插入为变量。

<?php
define("T1","one");
define("T2","two");

$test='number %2$s';

$test=sprintf($test, T1,T2);

echo $test;

See it working

【讨论】:

  • 好的,所以我不能在 heredoc 声明中这样做,因为这实际上是重点。
  • @user1209203:那你为什么不使用 nowdoc 呢?
猜你喜欢
  • 2014-07-02
  • 2013-11-10
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
  • 2015-02-18
  • 2017-12-04
相关资源
最近更新 更多