开发者

Question regarding embedding variables in double-quoted strings in PHP

Are the following instructions equivalent?

# 1
$str = "$var1$var2</td>";

# 2
$str = "$var1" . "$var2" . "</td&开发者_运维问答gt;";

EDIT: Thank you all.

header('Location:Question regarding anonymous methods as class members);


Essentially, but within a string it's recommended to contain the vars in {}:

$str = "{$var1}{$var2}</td>";

This is also useful because it allows you to do things like:

$str = "{$obj1->getName()}{$obj1->getDescription()}</td>";


You end up with the same string but the double quotes around each variable is superfluous. You could eliminate them and have:

$str = $var1 . $var2 . '</td>';

Most syntax highlighters color variables outside of strings different than strings, making it easier to scan.


Yes they are equivalant. When writing strings and putting variables in them, it is always preferable to write whatever is most readable (and actually works) by you or the team you work with. Ignore anyone who talks about time taken to parse single quoted strings Vs double quoted strings, this is micro-optimisation and the Root Of All Evil.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜