<?php
define('my_const', 100);
echo <<<MYECHO
<p>The value of my_const is {my_const}.</p>
MYECHO;
?>
如果我在括号内放置一个变量,则会打印出来。但不是常数。我该怎么做? 感谢的!
答案 0 :(得分:43)
我知道这已经得到了回答,但你也可以通过将常量的值赋给变量来解决问题。
我个人这样做是因为如果你的字符串中有很多常量,那么你的sprintf调用可能会非常混乱。然后更难以扫描字符串并查看正在做什么。另外,通过单独分配变量,您可以看到什么是值。
一个例子是:
$const = CONST;
$variable = VARIABLE;
$foo = (new Foo)->setFooProperty(12)->getFooProperty();
$bar = (123 - 456) * 10;
$ten = 1 + 2 + 1 + (5 - 4);
<<<EOD
Lorem ipsum dolor sit amet, **$variable** adipiscing elit.
Duis gravida aliquet dolor quis gravida.
Nullam viverra urna a velit laoreet, et ultrices purus condimentum.
Ut risus tortor, facilisis sed porta eget, semper a augue.
Sed adipiscing erat non sapien commodo volutpat.
Vestibulum nec lectus sed elit dictum accumsan vel adipiscing libero.
**$const** vehicula molestie sapien.
Ut fermentum quis risus ut pellentesque.
Proin in dignissim erat, eget molestie lorem. Mauris pretium aliquam eleifend.
**$foo** vitae sagittis dolor, quis sollicitudin leo.
Etiam congue odio sit amet sodales aliquet.
Etiam elementum auctor tellus, quis pharetra leo congue at. Maecenas sit amet ultricies neque.
Nulla luctus enim libero, eget elementum tellus suscipit eu.
Suspendisse tincidunt arcu at arcu molestie, a consequat velit elementum.
Ut et libero purus. Sed et magna vel elit luctus rhoncus.
Praesent dapibus consectetur tortor, vel **$bar** mauris ultrices id.
Mauris pulvinar nulla vitae ligula iaculis ornare.
Praesent posuere scelerisque ligula, id tincidunt metus sodales congue.
Curabitur lectus urna, porta sed molestie ut, mollis vitae libero.
Vivamus vulputate congue **$ten**.
EOD;
答案 1 :(得分:32)
define('my_const', 100);
$string = <<< heredoc
<p>The value of my_const is %s.</p>
heredoc;
$string = sprintf($string, my_const);
答案 2 :(得分:12)
这是一个小技巧,允许双引号字符串和heredocs包含花括号语法中的任意表达式,包括常量和其他函数调用。它使用的事实是函数名称可以赋给变量,然后在heredoc中调用:
<?php
// Declare a simple function
function _placeholder($val) { return $val; }
// and assign it to something short and sweet
$_ = '_placeholder';
// or optionally for php version >= 5.3
// Use a closure (anomynous function) like so:
$_ = function ($val){return $val;};
// Our test values
define('abc', 'def');
define('ghi', 3);
$a=1;
$b=2;
function add($a, $b) { return $a+$b; }
// Usage
echo "b4 {$_(1+2)} after\n"; // outputs 'b4 3 after'
echo "b4 {$_(abc)} after\n"; // outputs 'b4 def after'
echo "b4 {$_(add($a, $b)+ghi*2)} after\n"; // outputs 'b4 9 after'
$text = <<<MYEND
Now the same in heredoc:
b4 {$_(1+2)} after
b4 {$_(abc)} after
b4 {$_(add($a, $b)+ghi*2)} after
MYEND;
echo $text;
答案 3 :(得分:8)
您还可以使用get_defined_constants功能。它将所有当前定义的常量放回到数组中,您可以在HEREDOC字符串中使用
// Let's say there is FOO and BAR defined
$const = get_defined_constants();
$meta = <<< EOF
my awesome string with "{$const['FOO']}" and "{$const['BAR']}" constants
EOF;
答案 4 :(得分:0)
您可以使用“ 恒定”功能。
例如:
<?php
define('CONST1', 100);
define('CONST2', 200);
$C= 'constant';
echo <<<MYECHO
<p>The value of CONST1 is: {$C('CONST1')},
and CONST2 is:{$C('CONST2')}.</p>
MYECHO;
?>
答案 5 :(得分:-3)
将您定义的变量放入简单变量中,并将其包含在heredoc中,如下例所示:
<?php
define('my_const', 100);
$variable = my_const;
echo <<<MYECHO
<p>The value of my_const is {$variable}.</p>
MYECHO;
?>