A brief summary of the difference between single quotes and double quotes in a PHP string.
Today, a friend asked me about the difference. Let's review it by the way.
Single quotes and double quotes are different:
- "" Fields in double quotation marks are interpreted by the compiler and then output as HTML code.
- ''Is output directly without being interpreted in single quotes.
- The time for parsing single quotes is faster than that of double quotes.
- Single quotation marks support \ escape characters, and double quotation marks support more escape characters.
$ Hello = 3; echo "hello is $ hello"; // print the result: hello is 3 echo 'Hello is $ hello'; // print the result: hello is $ hello echo "hello is $ hello \ n"; // print the result: hello is 2 (line feed) echo 'Hello is $ hello \ n '; // print the result: hello is $ hello \ n
PS:
Today, I saw foreigners mention the single quotes of PHP, and some interesting things are mentioned. The excerpt is as follows:
After installing the PHP extension Vulcan Logic discycler, you can see the intermediate code generated by PHP,
First:
echo "This is a string";
Will change:
ECHO 'This is a string'
While
echo 'This is a string';
To
ECHO 'This is a string'
, Is the same
If yes
echo "This is a $variable";
The OPCODE generated by PHP is
INIT STRING ~0 2 ADD_STRING ~0 ~0 'This' 3 ADD_STRING ~0 ~0 ' ' 4 ADD_STRING ~0 ~0 'is' 5 ADD_STRING ~0 ~0 ' ' 6 ADD_STRING ~0 ~0 'a' 7 ADD_STRING ~0 ~0 ' ' 8 ADD_VAR ~0 ~0 !0 9 ECHO ~0
While
echo "This is a " . $variable;
Will become
CONCAT ~0 'This is a ' !0 2 ECHO ~0
As you can see, the speed is much faster.