回声中的回声 - 它是否有效


echo inside an echo - does it work?

我已经很久没有做过php了,所以很抱歉这个愚蠢的问题。

这是我当前的代码,我正在尝试将 URL 另存为变量,以便我可以将其插入回显中,但它似乎不起作用,因为没有任何内容出现:

<?php ob_start();
echo get_post_meta($post->ID, 'oldurl', true);
$old_url = ob_get_contents();
ob_end_clean();
?>
<?php echo do_shortcode('[fbcomments][fbcomments url="$old_url" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>

我已经回显了$old_url,可以看到它具有正确的值,但是如何使用url="$old_url"将值插入echo do_shortcode中?

这也不起作用:

<?php echo do_shortcode('[fbcomments][fbcomments url="echo $old_url;" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>

您需要切换引号。单引号按原样打印所有内容。双引号将处理变量。此外,回声中不需要回声。

<?php echo do_shortcode("[fbcomments][fbcomments url='$old_url' width='375' count='off' num='3' countmsg='wonderful comments!']"); ?>    

另一种在不切换引号的情况下做到这一点的方法是打破语句:

<?php echo do_shortcode('[fbcomments][fbcomments url="'.$old_url.'" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
变量

不会用单引号替换...

<?php echo do_shortcode('[fbcomments][fbcomments url="' . $old_url . '" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>

单引号不允许变量解析。例如:

$var = 'Hello';
echo 'The content of my var is : $var';
// Will output : "The content of my var is : $var"
echo "The content of my var is : $var";
// Will output : "The content of my var is : Hello"

所以你必须使用双引号或使用连接运算符:.