53

考虑:

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

如果我在大括号内放置一个变量,它会打印出来。但不是常数。我该怎么做?

4

6 回答 6

59

您还可以通过将常量的值分配给变量来解决问题。

就我个人而言,我这样做是因为如果您的字符串中有很多常量,那么您的 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;
于 2013-01-02T14:06:25.393 回答
42

使用sprintf()

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

$string = sprintf($string, my_const);
于 2012-04-06T08:54:57.273 回答
20

这是一个小技巧,允许双引号字符串和 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;
于 2016-09-22T21:56:20.627 回答
12

您还可以使用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;
于 2015-06-11T10:13:31.240 回答
8

您可以使用“常量”功能。

例如:

    <?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;
    ?> 
于 2020-11-26T02:37:24.397 回答
0

将您定义的变量放入简单变量中,然后将其包含在 heredoc 中,如下例所示:

<?php
define('my_const', 100);
$variable = my_const;
   echo <<<MYECHO
      <p>The value of my_const is {$variable}.</p>
MYECHO;
?>
于 2014-11-15T11:48:02.043 回答