如何在变量的字符串中获得一个常量来计算?
$foo = "like";
define('BAR', 'fish');
// None of these give me 'I like to eat fish on Tuesday.'
echo "I $foo to eat BAR on Tuesday.";
echo "I $foo to eat {BAR} on Tuesday.";
echo 'I $foo to eat BAR on Tuesday.';
// This works but is undesirable
echo 'I $foo to eat '.BAR.' on Tuesday.';
答案 0 :(得分:2)
我知道这种方法的唯一方法(你可能已经知道)是:
<?php
$foo = "like";
define('BAR', 'fish');
$constants = get_defined_constants();
echo "I $foo to eat {$constants['BAR']} on Tuesday.";
?>
打印:
I like to eat fish on Tuesday.
答案 1 :(得分:1)
据我所知,这是不可能的。在SO处查看这个答案,它包含有用的解决方法,如果连接也是如此不受欢迎你! Include constant in string without concatenating
答案 2 :(得分:-2)
当然没有办法。没有必要。