Should I use php quote escapes for single quotes or use double quotes in arrays?

南笙酒味 提交于 2019-11-28 03:15:35

问题


I realized that I can have problems with single quotes in php arrays:

<?php
$lang = array(
    'tagline' => 'Let's start your project',
    "h1" => "About Me"
);
?>

So I changed it to double quotes:

<?php
$lang = array(
    "tagline" => "Let's start your project",
    "h1" => "About Me"
);
?>

Should I use "php quote escapes", instead of what I just did? (by the way how to write "php quote escapes"?)


回答1:


First of all, some people will say that simple-quoted strings are faster that double-quoted strings ; you should not care about that kind of micro-optimization : it will not make any difference for your application.


The difference between simple-quoted and double-quoted strings is :

  • with double-quoted strings, there is variable interpolations
  • with double-quoted strings, you can use some special characters like \n, \t, ...
  • with single-quoted strings, you have simple-quotes to escape.

For reference, in the PHP manual :

  • Single quoted
  • Double quoted


I would that that, in the general matter, it's mostly a matter of personnal preferences...

Personnaly, in a situation such as the one you described, I would use a double-quoted string, like you did : it make the code easier to both write and read, as you don't have to escape that quote.




回答2:


Do whatever is most readable, assuming you don't need the double-quote special features. For me, that's using whichever quotes for the string that appear the least in the string.



来源:https://stackoverflow.com/questions/2317959/should-i-use-php-quote-escapes-for-single-quotes-or-use-double-quotes-in-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!