php 将常量(非变量)插入'heredoc'

osh3o9ms  于 2023-01-29  发布在  PHP
关注(0)|答案(7)|浏览(207)

考虑:

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

如果我在括号里放一个变量,它会打印出来,但是常量不会,我该怎么做呢?

zzlelutf

zzlelutf1#

也可以通过将常量的值赋给变量来解决这个问题。
就我个人而言,我之所以这样做是因为如果你的字符串中有很多常量,那么你的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;
z9gpfhce

z9gpfhce2#

使用sprintf()

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

$string = sprintf($string, my_const);
ycl3bljg

ycl3bljg3#

下面是一个小技巧,允许双引号字符串和heredoc包含任意的表达式,包括常量和其他函数调用,它利用了函数名可以被赋给变量,然后在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;
gv8xihay

gv8xihay4#

你也可以使用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;
8qgya5xd

8qgya5xd5#

您可以使用"常量"功能。
例如:

<?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;
    ?>
xdnvmnnf

xdnvmnnf6#

把你定义的变量放入简单变量中,并使用include it in heredoc,如下例所示:

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

cs7cruho7#

不是每个人都喜欢使用速记echo标记,但这仍然是一个选择:

<?php
   define('my_const', 100);
?>
      <p>The value of my_const is <?= my_const ?>.</p>

相关问题