PHP中单引号和双引号字符串有什么区别?
PHP编程中的单引号或双引号用于定义字符串。但是,这两者之间有很多不同之处。单引号字符串:这是定义字符串的最简单方法。当您希望字符串与编写时完全相同时,可以使用它。所有转义序列,如\r或\n ,将按指定输出,而不是具有任何特殊含义。在某些情况下,单引号通常更快。特殊情况是,如果要显示字面量单引号,请使用反斜杠 (\)对其进行转义,如果要显示反斜杠,则可以使用另一个反斜杠 (\\)对其进行转义。
下面的程序说明了单引号字符串:
方案一:
php
php
输出:
I am a geek.
It'll be interesting to know about the string.
A \ is named as backslash.
This is a portal for $string.
This is a portal for \n geeks.
双引号字符串:通过使用双引号, PHP代码被迫计算整个字符串。双引号和单引号的主要区别在于,通过使用双引号,您可以直接在字符串中包含变量。它解释转义序列。每个变量都将被其值替换。
下面的程序说明了双引号字符串:
方案二:
PHP
输出:
I am a geek.
It'll be interesting to know about the string.
This is a simple string.
The word is ABC.
让我们以表格形式了解差异 - : Syntax -: ‘string’ Syntax -: “string” Example -: ‘GeeksforGeeks’ Example -: “GeeksforGeeks” Single-Quoted Strings Double-Quoted Strings 1. It is a way to specify a string in PHP It is also a way to specify a string in PHP 2. 3. In Single-Quoted strings, the Escape sequence does not expands In Double-Quoted strings, the Escape sequence expands. 4. In Single-Quoted strings, the variable’s name does not expands In Double-Quoted strings, the variable’s name also expands 5.