douzhan8303 2010-03-11 11:32
浏览 60
已采纳

正确使用双引号和单引号?

I'm talking about the performance increase here. From all I know you can echo variables in double quotes ("), like so:

<?php

echo "You are $yourAge years old";

?>

But single quotes will just return You are $yourAge years old. But what about performance differences? I've always gone by the rule that single quotes are faster because the PHP interpreter doesn't have to search through the string for variables. But I'm seeing more and more blog and forum posts on the web saying differently.

Does anyone actually have any information on this subject? Perhaps benchmark tests or something?

  • 写回答

4条回答 默认 最新

  • duanjue9889 2010-03-11 11:36
    关注

    According to the PHP Benchmark, the difference is extremely negligible:

    single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a'
    235 µs
    
    double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a";
    226 µs
    

    Even if the differences were a multiple of what they are, they would not be relevant for real-life performance IMO. Database and file operations will take dozens, if not hundreds of times more time. That's not to say your question isn't totally valid, but it's not a big deal when optimizing your code.

    Readability is much, much more important.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部