When should you use single or double quotes in PHP? [duplicate]

烂漫一生 提交于 2019-12-08 14:21:46

问题


Forgive the bluntness of the question, but I've noticed really you can use either single or double quotes to begin with, along as you follow the suite throughout.

Someone once told me to use double quotes when echoing, outputting or returning, and single quotes for everything else.

But I'd like to know, is there a solid reason when to only use single quotes, or vice versa and not the other?


回答1:


The only difference is that double quoted strings interpret embedded variables and a number of escape sequences, while single quoted strings do not. E.g.:

'This is $a \n string'

This is literally the string "This is $a \n string".

"This is $a \n string"

This is a string containing the value of variable $a and a line break.

Use both as appropriate. If neither escape sequences nor variables are of interest to you, I'd default to single quoted strings; with one exception: if you need single quotes in the string, then double quotes are "cleaner". Compare:

'I don\'t care.'
"I don't care."


来源:https://stackoverflow.com/questions/25305869/when-should-you-use-single-or-double-quotes-in-php

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