开发者

Is it bad habit which is using variable name in double quote?

$foo= 'dudes';
$bar= 'whats up';
<?php echo 'hey,'.$foo.' '.$bar.'?'; ?>
<?php echo "hey, $foo $bar?"; ?>

is

<span style="color:#993333">
<?php echo "hey, $foo $bar?"; ?>
</span>

slower than

<span style="color:#开发者_JAVA技巧339933">
<?php echo 'hey,'.$foo.' '.$bar.'?'; ?>
</span>

?


Yes. It is slightly faster to use single quotes.

This is because when you use double quotes PHP has to parse to check if there are variables in there.

Speed difference in using inline strings vs concatenation in php5?


Nope it is not a bad habit nor slower.

However, when using PHP templating, try to avoid printing several variables at once, to keep things straight.
I'd make it this way:

hey, <?=$foo?> <?=$bar?>?

But it's still matter of style as there are no other reasons to prefer one over another

A note. It's a pity to see numerous answers from not-so-experienced participants, who speaking not from their own real life experience but just repeat some durable rumor, one after another.

Also, I'd say that the question itself has very poor reasoning. If you asks what is slower - you asks wrong question. For the most questions such difference just doesn't matter and just waste of time. Performance tuning is a process, not some "mysterious knowledge". One have to learn how to profile their app and get some basic experience to ask sensible performance related questions. Otherwise there will be no any good.


Yes. It is slightly slower to type constructs with single quotes.

There is no difference in runtime speeds. The only difference is in tokenizing, where single quotes are processed negligibly faster.

I've run a test once (PHP 5.3.2, amd64, linux) to measure some difference, very unscientifically. Single quotes were 14% faster, but only because I pumped 1,000,000,000,000 strings through eval. Otherwise it's hard to measure. And it is seriously irrelevant for real world applications, since you never have that many strings.

Hencewhy the use of single quotes also raises the perception of unintelligent design, when there is another syntax construct that's pretty optimized and way more readable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜