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

后端 未结 2 478
北海茫月
北海茫月 2020-12-19 02:41

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

 \'Let\'s start your project\',
    \"h1\"         


        
2条回答
  •  青春惊慌失措
    2020-12-19 03:21

    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.

提交回复
热议问题