Punctuation marks in programming languages are not like when we speak, we can add them casually and others will know what you want to say, but computers are different. Improper use will cause errors and cause you a lot of trouble. In PHP The use of single quotes and double quotes is a problem that novices often encounter. This article explains the difference between the two in detail. I hope it will be helpful to novices!
In PHP, a string is usually defined within a pair of quotation marks, such as:
'I am a string in single quotes'
"I am a string in double quotes"
The PHP parser uses pairs of quotation marks to determine a string. Therefore, all strings must use the same single or double
Quotation marks to define start and end. For example, the following string definition is illegal:
"I am not a valid string since I have unmatching quote marks'
'Me neither!"
When defining a string, only one kind of quotation mark is considered as the delimiter, that is, single quotation mark or double quotation mark. So, if a string is represented by a double quote
, then only double quotes are parsed by the parser. This way, you can include any other character within the double-quoted string, even single-quoted
Number. The following quotation mark strings are legal: www.2cto.com
$s = "I am a 'single qThe above example attempts to include double quotes within a double quote string, and the parser considers the string to be terminated when it encounters the second double quote
It’s over. To achieve the purpose of including quotation marks, the parser must ignore its original meaning when encountering ordinary quotation marks in the string. We use the quotation mark
Add a backslash in front of it to tell PHP that this quote is part of the string. The correct representation is like this:
"Why doesn't "that" work?"
A common problem in English strings is the use of apostrophe ', because it is a single quote, and it is very common in English strings
(English possessive case). You must be careful with these characters:
'You'd better escape your a post rophes'
You can see that backslash has its special meaning in the string. When we need to include the backslash itself in the string, we need to include it in
This symbol is preceded by an extra backslash. For example:
$file = "c: windows system.ini";
echo $file; // The print result is: c: windows system.ini
$file = "c:\windows\system.ini";
echo $file; // The print result is: c:windowssystem.ini
uote string' inside a double quote string";
$s = 'I am a "double quote string" inside a single quote string';