In PHP, you can use single quotes or double quotes to define a string. In ordinary use, there is no breakdown of the differences between the strings defined by these two symbols. Today I wrote a few lines of code as an example of "pass-by-reference assignment". In this code, both single quotes and double quotes are used to define strings (Please pay attention to the fourth and sixth lines in the code snippet below) , something wonderful happened after execution.
$var1="ChrisMao";//Assignthevalue"ChirsMao"to$var1
$var2=& $var1;//Reference$var1via$var2
echo'Thevalueof$var2is:', $var2 ,"
";//$var2and$var1havethesamevalue"ChrisMao"
$var2=' mynameis$var2' ;//Modify$var2,thesametime$var1wasmodified
echo'Thevalueof$var1is:' ,$ var1,"" ="myNewnameis $var1";
//Modify$var2,thesametime$var1wasmodifiedecho'Thevalueof$var2is:' ,$var2
,"
";//Thevalueof$var1is"myNewnameismynameis$var2" echo' Thevalueof$var1is:',$var1
,"
";// Thevalueof$var1is "myNewnameismynameis$var2"The picture below is a screenshot of the execution resultI wonder if you have noticed anything after seeing this result. In the strings on the right side of the assignment statements in the fourth and sixth lines of the code, the same variable names appear in the strings on the right side of the assignment statements, but one sentence uses single quotes and the other uses double quotes . Before execution, the expected output should be to output the variable name as a string instead of outputting the variable value. But after execution, it was found that the output was different from previous expectations. The assignment using single quotes will output the variable name in the string as it is, while the assignment statement using double quotes will replace the variable name with the actual value of the variable and output it.
The most important thing about double-quoted strings is that the variable name will be replaced by the variable value Replacement
. It’s all the fault of not reading carefully!
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.The above introduces the difference between single quotation marks and double quotation marks in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.