Difference between single quotes and double quotes in PHP

Source: Internet
Author: User

In PHP, you can use single or double quotation marks to define a string. In normal use, there is no difference between the two symbol-defined strings. Today, I wrote several lines of code to illustrate the example of "assignment by address". In this code, I used single quotation marks and double quotation marks to define strings (Note the lines 4 and 6 in the code snippet below.), But after the execution, a wonderful thing happened.

$ Var1 = "Chris Mao"; // assign the value "chirs Mao" to $ var1
$ Var2 = & $ var1; // reference $ var1 via $ var2
Echo '<B> the value of $ var2 is: </B>', $ var2, "<br> "; // $ var2 and $ var1 have the same value "Chris Mao"
$ Var2 = 'My name is $ var2' ;// Modify $ var2, the sametime $ var1 was modified
Echo '<B> the value of $ var1 is: </B>', $ var1, "<br> "; // The value of $ var1 is "My name is $ var2"
$ Var2 = "My new name is $ var1" ;// Modify $ var2, the sametime $ var1 was modified
Echo '<B> the value of $ var2 is: </B>', $ var2, "<br> "; // The value of $ var1 is "My new name is my name is $ var2"
Echo '<B> the value of $ var1 is: </B>', $ var1, "<br> "; // The value of $ var1 is "My new name is my name is $ var2"

Is the execution result

 

I wonder if you have found anything in this result.The variable name is the same as the variable name in the code snippet in the right string of the value assignment statement in the fourth and sixth lines of the Code, but it is a single quotation mark, and the other is a double quotation mark.. Before execution, the expected output should be to output the variable name as the string, rather than the variable value. However, after execution, the output is different from the previous expectation.The variable name in the string is output using the single quotation mark assignment, while the variable name is replaced by the actual value output for the variable using the double quotation mark assignment statement.

I rummaged through the book again to explain the Chapter of the string variable and read it carefully. I found that the book described this phenomenon early:The most important aspect of a double quotation mark string is that the variable name is replaced by the variable value.. It's all about reading books!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.