将常量(非变量)插值到"heredoc"中 [英] Interpolate a constant (not variable) into 'heredoc'

查看:62
本文介绍了将常量(非变量)插值到"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);

这篇关于将常量(非变量)插值到"heredoc"中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆