如果我在大量PHP代码中,而不是从PHP中出来,我通常编写包含如下变量的代码:
echo "This is how I use a ".$variable." inside a string";
Run Code Online (Sandbox Code Playgroud)
但是这样实际上脱离PHP是否更有效:
?>
Should I instead use the <? echo $variable; ?> like this
<? // then back into PHP
Run Code Online (Sandbox Code Playgroud)
整个页面中会有很多像这样的代码实例,因为我用动态内容填充页面,或者这是一个泛化的过多?
我只建议在回显HTML时突破php标签,而不仅仅是字符串.
这对于一个字符串很好:
// You don't need to concat, double quotes interpolate variables
echo "This is how I use a $variable inside a string";
Run Code Online (Sandbox Code Playgroud)
但是对于HTML,我个人喜欢这样做:
<?php //... ?>
<div>
<span>This is how I use a <?=$variable?> inside HTML</span>
</div>
<?php //... ?>
Run Code Online (Sandbox Code Playgroud)