PHP中的单引号和双引号字符串有什么区别? [英] What is the difference between single-quoted and double-quoted strings in PHP?

查看:116
本文介绍了PHP中的单引号和双引号字符串有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有些困惑,为什么我会在PHP中看到一些代码,其中的字符串用单引号引起来,有时用双引号引起来.

I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.

我只是用.NET或C语言知道,如果用单引号引起来,那意味着它是一个字符,而不是字符串.

I just know in .NET, or the C language, if it is in a single quote, that means it is a character, not a string.

推荐答案

PHP字符串 不仅可以用两种方式指定,还可以用四种方式指定.

PHP strings can be specified not just in two ways, but in four ways.

  1. 用单引号引起来的字符串 将几乎完全按原样"显示内容.变量和大多数转义序列将不会被解释.唯一的例外是,要显示文字单引号,可以使用反斜杠\'对其进行转义,而要显示反斜杠,则可以使用其他反斜杠\\(字符串进行了解析).
  2. 双引号字符串 将显示许多转义字符(包括一些正则表达式),并将对字符串中的变量进行求值.这里重要的一点是,您可以使用花括号将要评估的变量的名称隔离.例如,假设您有变量$type,而您想echo "The $types are".这将查找变量$types.要解决此问题,请使用echo "The {$type}s are"您可以将左括号放在美元符号之前或之后.看看字符串解析了解如何使用数组变量等.
  3. Heredoc 字符串语法的作用类似于双引号字符串.它以<<<开头.在此运算符之后,提供了一个标识符,然后提供了换行符.字符串本身紧随其后,然后再次使用相同的标识符以关闭引号.您无需使用此语法对引号进行转义.
  4. Nowdoc (自PHP 5.3.0起)字符串语法的工作原理与单引号字符串相同.区别在于,甚至不必转义单引号或反斜杠. nowdoc使用与heredocs相同的<<<序列进行标识,但其后的标识符用单引号引起来,例如<<<'EOT'. nowdoc中没有解析.
  1. Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
  2. Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you want to echo "The $types are". That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such.
  3. Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
  4. Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.

注释: 单引号内的单引号和双引号内的双引号必须转义:

Notes: Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:

$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";

速度:
我不会过分强调单引号比双引号更快.在某些情况下,它们可能更快.这是文章 基准页面 具有单引号和双引号的比较.大多数比较是相同的.有一种比较,双引号比单引号慢.

Speed:
I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here's an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3 (Useless Optimizations toward the bottom, section C). Also, this benchmarks page has a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.

这篇关于PHP中的单引号和双引号字符串有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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