Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php变量赋值以显示html&;php变量_Php_Concatenation - Fatal编程技术网

php变量赋值以显示html&;php变量

php变量赋值以显示html&;php变量,php,concatenation,Php,Concatenation,我有一个PHPNoob的时刻,我在寻找一个更小更紧凑的解决方案来连接“$content”变量,它作为一个外部“layout.php”文件中的主要内容被回显 解决了这个问题,将php调用括在括号中得到了我想要的最终结果 $content = '<div class="comment_box">' . ($comment->getComments() ) . '</div>'; $content=''。($comment->getComments())。'; 使

我有一个PHPNoob的时刻,我在寻找一个更小更紧凑的解决方案来连接“$content”变量,它作为一个外部“layout.php”文件中的主要内容被回显


解决了这个问题,将php调用括在括号中得到了我想要的最终结果

$content = '<div class="comment_box">'  .  ($comment->getComments() ) .  '</div>';
$content=''。($comment->getComments())。';
使用sprintf()

例如:

$content = sprintf('<div class="comment_box">%s</div>', $comment->getComments());
$content=sprintf('%s',$comment->getComments());

您的原始代码应该完全相同。如果你看到的是源代码,这是一个不同的问题。为什么这与他的原始代码有任何不同?因为他问:我正在寻找一个更小更紧凑的解决方案来连接“$content”我认为他的问题在于他在网页上看到了源代码。你是对的,OP的问题中有两个问题。编辑OP以消除混淆。这不是两个问题。谢谢你的回复,我真不敢相信我忘记了sprintf();如果您正在寻找关于编码风格的建议,正确的发布位置是CodeReview.stackexchange.com。这是为了帮助解决代码中的问题。请注意,谢谢。
$content = sprintf('<div class="comment_box">%s</div>', $comment->getComments());