什么时候应该在 PHP 中使用单引号或双引号? [英] When should you use single or double quotes in PHP?

查看:43
本文介绍了什么时候应该在 PHP 中使用单引号或双引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅这个问题的坦率,但我注意到您确实可以使用单引号或双引号开头,并在整个套件中进行操作.

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?

推荐答案

唯一的区别是双引号字符串解释嵌入的变量和一些转义序列,而单引号字符串不解释.例如:

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 $a \n string".

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

"This is $a \n string"

这是一个包含变量 $a 的值和一个换行符的字符串.

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."

这篇关于什么时候应该在 PHP 中使用单引号或双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆