开发者

Putting $variable string into "string"

I have question because i think i doing unnecessary job.

e.g.

$phone = "222-333-444"
echo "Pho开发者_如何学Gone number is " . $phone;

$phone['2'] = "222-333-444"
echo "Phone number is " . $phone['2'];

Please explain is there ANY ANY ANY reason to do it the way above rather than"

$phone = "222-333-444"
echo "Phone number is $phone";

$phone['2'] = "222-333-444"
echo "Phone number is $phone['2']";


Both are right ways to print a variable. There's no right or wrong, just different approaches.


No, actually both are equivalent. I personally always write this, because for me it is far more explicit:

echo "Phone number is " . $phone['2'];


The only reason would be consistency and clarity. Concatenation is faster than interpolation, but the difference is so negligible it should not define your choice. I prefer the second personally.


No, there is not difference.

second example should be like this:

$phone = "222-333-444"
echo "Phone number is {$phone}";

$phone['2'] = "222-333-444"
echo "Phone number is {$phone['2']}";


No, there is no reason at all, unless your variable is an array and you need to access a specific index.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜