Interpolate constant (not variable) into heredoc?

♀尐吖头ヾ 提交于 2019-11-29 01:28:06

问题


<?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? Thank's!


回答1:


Use sprintf()

define('my_const', 100);
$string = <<< heredoc
      <p>The value of my_const is %s.</p>
heredoc;

$string = sprintf($string, my_const);



回答2:


I'm aware that this has been answered already, but you can also approach the problem by assigning the value of the constant to a variable.

Personally I do it that way because if you have lots of constants in your string then your sprintf call can be quite messy. It's also then harder to scan through the string and see what is doing what. Plus, by assigning the variables individually, you can see what is taking on what value.

An example would be:

$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;



回答3:


Here is an little trick to allow double-quoted strings and heredocs to contain arbitrary expressions in curly braces syntax, including constants and other function calls. It uses the fact that a function name can be assigned to a variable and then called within 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;



回答4:


You can also use get_defined_constants function. It puts back all currently defined constants in an array, which you can use in your HEREDOC string

// 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;



回答5:


Put your defined variable into simple variable and use include it in heredoc just as in following example:

<?php
define('my_const', 100);
$variable = my_const;
   echo <<<MYECHO
      <p>The value of my_const is {$variable}.</p>
MYECHO;
?>


来源:https://stackoverflow.com/questions/10041200/interpolate-constant-not-variable-into-heredoc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!