Detailed description of double quotation marks in php

Source: Internet
Author: User
Php summary Basic Knowledge Summary 1: about the double quotation mark brackets in php. in php, strings can be expressed by single quotation marks, double quotation marks, and outputs. Example: & lt ;? Phpecho & quot; aabbcc & quot; echoaabbcc;... php summary

Basic Knowledge Summary 1: Summary of the double quotation marks and curly brackets in php
In php, strings can be expressed by single quotes, double quotation marks, and outputs.
For example:

Php code

 

Their output is the same. Aabbcc is printed.
However, if aabbcc is assigned to a variable, how can it be output?

Php code

 

Because "" double quotation marks have an interpreted function in php, it will explain this string. if there is a variable in the string or an escape character, it will explain the output at the beginning. The single quotation marks are not interpreted, that is, when you echo '$ a'; or echo' \ n '; it only outputs $ a \ n as a string and does not output the variables or escape characters you want to interpret.
What is an explanatory function? In fact, this is only because when you use double quotation marks to output strings, the system will match the $ symbol in regular expressions to identify variables. The escape character is also...
There is another situation in which the above part is escaped. We output 'single quotation marks and double quotation marks respectively.

Php code

Echo '\ ''; echo"' "; // double quotation marks are enclosed in single quotation marks, avoiding the use of \ escape echo" \ ""; echo '"'; // for double quotation marks, single quotes avoid escape operations.

Therefore, through the above explanation, we can regard it as, when we only output strings, there is no need to explain, we should directly use single quotes to omit an explanation process. although there is no significant improvement in the running speed, the operating efficiency is higher than double quotation marks in principle. When the output content needs to be explained, we can use double quotation marks. For example, echo "aaaaa $ a"; the output is aaaaaaabbcc.
A problem occurs here. Please refer to the code

Php code

$ Res = 'XXX'; // Step 1 echo "aaa $ resbbbb"; // Step 2 // in this way, php is treated as $ resbbbb as a variable during parsing, naturally, an error will be reported. // How can this situation be avoided. Echo "aaa {$ res} bbbb"; // Step 3

In the second step, I actually want the variable $ res. But the obtained result is $ resbbbb,
This is because zend uses regular matching for parsing. I don't know if you only want res. Regular expressions recognize Variables. as long as it is a variable character (_ character number, and not the beginning of a number), the system regular expressions always match. If there are spaces, the variable recognition ends naturally. $ And space are variable names. (Of course, as long as the space here is not a character of the variable naming convention)
Echo "aaa $ res bbbb"; this statement runs normally, but there is a space in the output string.
I don't want this extra space?
Use the code echo "aaa {$ res} bbbb" in step 3 ";
In this way, two curly braces are also found in the regular expression during zend parsing. The characters in {} will then use regular double quotation marks for regular matching. So that $ res is directly found, and curly braces are not output.
What if the braces are {aaa $ res? That is, echo "aaa {ggg $ res} bbbb"; that is, not only variables or not variables are included in curly brackets.
At this time, the system will re-determine to use the regular expression of double quotation marks to parse the entire string to find the variable. In this case, the "}" after res does not belong to the variable name.
So the above output will be: aaa {ggg xxx} bbbb
Conclusion: When strings and variables are concatenated and output. If the running efficiency is high, the variable is included in. Of course, there should be no non-variables in {}, otherwise it will be slower than using "" directly.

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.