PHP single and double quotes curly braces detailed

Source: Internet
Author: User
PHP Summary

Summary of basic knowledge: a summary of single-quote double-quote curly braces in PHP
In PHP, strings can be represented by single quotes or double quotes, output, and so on.
For example:

PHP code

<?php        echo  "AABBCC";           Echo ' AABBCC ';  ? >

Their output is the same. Will print out AABBCC
But if AABBCC is assigned to a variable, how does it output?

PHP code

<?php   $a = "AABBCC";//or $a= ' AABBCC ';  echo $a;  echo "$a"; Here you will find that their output is AABBCC  //If it is output and enter it  echo $a. " \ n ";  echo "$a \ n";   Both of these methods are possible. The output is AABBCC  for what,    ?>

Because the "" double quotes have an explanatory function in PHP, it will explain the string if there is a variable or escape character in the string and it will explain the output. and single quotes do not explain the function, that is, when you echo ' $a '; or echo ' \ n '; It will only output as a string $a \ n does not output the variable or transfer character you want to interpret well.
What is an explanatory function? In fact, this is simply, if you use double quotes to output a string, the system will go to the regular match $ symbol, thus identifying the variable. The escape character is also ...
There is one more case for escaping that piece of information. We output ' single quotes and double quotes ' separately

PHP code

echo ' \ ';  echo "'"; Enclose the single quotation mark in double quotation marks to avoid the use of the \ Escape    echo "\";  Echo ' ";  For double quotes, single quotes have an operation that avoids escaping.

So, by the above explanation, we can assume that when we just output the string, there is no need to explain the things, we should directly use the "single quotation mark so that we can omit an interpretation of the process, at the speed of operation although not visible elevation, but the principle of operating efficiency is higher than double quotation mark." We can use the "" double quotation mark when we need to interpret the content of our output. For example echo "aaaaa$a"; Output is AAAAAAABBCC
There's a problem here. Please look at the code

PHP code

$res = ' xxx ';    The first step  is echo "aaa$resbbbb";///second step//  this way, PHP in the parsing process, as $RESBBBB is a variable, will naturally error,  //How to avoid such a situation.  echo "aaa{$res}bbbb";//Step Three

The second step is that I want to $res variables. But what we get is $resbbbb,
Because the Zend is parsed with a regular match. Don't know you just want res. Regular recognition is a variable, as long as the character of the variable (_ character number, and not the beginning of the number), then the system will always match. If there are spaces then the variable recognition ends naturally. The variable name is between $ and the space. (Of course, the space here as long as it is not a variable name specification of the character line)
echo "Aaa$res bbbb"; This will work fine, except for a single space in the output string.
I do not want the extra space it?
Then use the third step of the code echo "aaa{$res}bbbb";
In this case, when Zend parsing, the same is true for finding two curly braces. The characters in {} will then use regular matching with normal double quotes. So we can find $res directly. And curly braces don't output
What about {AAA $res} in curly braces? That is: echo "aaa{ggg $res}bbbb"; That is, the curly braces are not just variables or variables.
At this point, the system will re-judge the entire string with double quotation marks to parse the lookup variable. At this point, the "}" after Res does not belong to the variable name.
So the above output will be: AAA{GGG xxx}bbbb
Summary: When strings and variables are stitched out. The more efficient is the {} enclosing variable. Of course {} Do not have a non-variable, or it will be more than "" directly caused by slow.

  • 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.