Xin Xing and you thoroughly interpret the difference between single quotes and double quotes

Source: Internet
Author: User

I thought that the difference between single quotes in PHP is so simple and the basic knowledge should be that every phper knows the difference, and there are still a lot of phper does not know, which is also very painful and painstaking, so, the difference between single quotes and double quotes in PHP is actually very simple.

When PHP parses double quotation marks, it is automatically analyzed by the analyzer and converted to the string once. Therefore, if the double quotation mark string contains variables, it is automatically replaced by variables, for example, the following code:

 
<? PHP $ Xin = "Xin Xing"; echo "$ Xin"; echo '$ Xin ';

The output of the above Code is as follows:

 
Xin Xing $ Xin

The reason is very simple, that is, when we use double quotation marks, it will be automatically parsed as Xin Xing. When we use single quotation marks, it will be output as is.

In fact, it is not just a variable, but some escape characters will also be parsed in double quotation marks. For example, let's look at the following code:

 
<? PHP $ Xin = "Xin \" star "; echo" $ Xin "; echo" <br/> "; echo '$ Xin ';
I think you should guess what the output is. That's right. The output content is as follows:

 
Xin "star $ Xin
Of course, there is a small misunderstanding. I don't know if you know it. That is, Let's guess what the following code will do? The Code is as follows:

 
<? PHP $ Xin = "Xin Xing"; echo "$ Xin hello ";
If the reader thinks that "Hello Xin Xing" is output, it indicates that it has never been used. In fact, its output is as follows:

 
(! ) Scream: Error suppression ignored (! ) Notice: undefined variable: Xin hello in D: \ MyApp \ Wamp \ www \ API. PHP on line 3 call stack # timememoryfunctionlocation10.0021138760 {main }().. \ API. PHP: 0

By reading the above error message, we know that it used to treat "Xin hello" as a variable for parsing. Obviously this variable does not exist, so our program has a bug, so how can we modify it? In fact, just add a space. The modified code is as follows:

 
<? PHP $ Xin = "Xin Xing"; echo "$ Xin hello ";
The output result is as follows:

 
Hello, Xin Xing

By the way, I forget to mention that it is a little faster to use single quotes, because it does not have the process of parsing variables, so it is easy to understand the reasons ..




Xin Xing and you thoroughly interpret the difference between single quotes and double quotes

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.