将常量(非变量)插值到"heredoc"中
问题描述:
考虑:
<?php
define('my_const', 100);
echo <<<MYECHO
<p>The value of my_const is {my_const}.</p>
MYECHO;
?>
如果我在大括号内放一个变量,它将打印出来.但不是常数.我该怎么办?
If I put a variable inside the braces, it prints out. But not the constant. How can I do it?
答
使用 sprintf()
define('my_const', 100);
$string = <<< heredoc
<p>The value of my_const is %s.</p>
heredoc;
$string = sprintf($string, my_const);