[PHP] Difference between single quotation and double quotation
Sep 7, 2020
PHP
beginner
programming study diary
#Programming study diary September 7, 2020 I use single quotation and double quotation in the same situation, but I will summarize what the difference actually is.
#Feature Single quotation: Treat the contents as a character string (handle variables and control character strings as just a character string) Double quotation: Treats the contents as a character string, but treats variables and control character strings as the original source
Since all single quotations are treated as character strings, single quotations are faster.
#Difference in speed When outputting a simple character string as shown below, the output content is the same.
//Double quotation
echo "Hello World";
echo'<br>';
// single quote
echo'Hello World';
Hello World
Hello World
As mentioned at the beginning, the operation processing speed is single quotation » double quotation </ font> when using variable expansion and when variable is simply assigned. (There is a difference of about 1.2 to 2 times) However, there is not much difference in speed between the case of character string concatenation and the case of character string comparison.
#Difference in operation
It was described at the beginning that the handling of variables and control character strings is different, but it will be explained using a concrete example. Control strings are characters used to perform special controls other than characters such as newlines (\ n
) and empty strings (nil
).
As shown in the specific example below, in the case of single quotation, the control character string and variables are output as they are as a character string. The double quotation is output as it is as the contents of the variable or the control character string.
$ str = "World";
echo'Hello $ str';
echo "Hello $ str";
echo "Hello {$ str}";
echo'Hello World \ n';
echo "Hello World \ n";
Hello $ str
Hello World
Hello World
Hello World \ n
Hello World (new line on display)
#Summary Although it is a small system, it is faster to use single quotation. (For large systems, it makes a big difference) Basically, use single quotations, and use double quotations when necessary (variable expansion, etc.).
#References [PHP]'(single quotation) or “(double quotation) should be used (PHP acceleration) Difference between double quotation and single quotation