php summary
Basic knowledge summary 1: Regarding single quotes, double quotes, curly braces in php, problem summary
In php, strings can be expressed in single quotes or double quotes to express, output, etc.
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 should it be output?
Php code
<?php $a = "aabbcc";//或者$a='aabbcc'; echo $a; echo "$a"; //在这里你会发现 他们的输出都是 aabbcc //如果是输出并且回车呢 echo $a."\n"; echo "$a\n"; //这两种方式都是可以的。输出都是aabbcc 为什么呢, ?>
is because "" double quotes have an interpretation function in php. It will interpret this string. If there are variables or escape characters in the string, it will also interpret it before outputting. Single quotes have no interpretation function, that is to say, when you echo '$a'; or echo 'n'; it will only output $a n as a string and will not output the interpreted variables or variables you want. Transfer characters.
What is the explain function? In fact, this is just that if you use double quotes to output a string, the system will regularly match the $ symbol to identify the variable. So are escape characters. . .
There is another situation regarding the escape above. We output ' single quotes and double quotes "
Php code
echo '\''; echo "'"; //双引号括住单引号,避免了再去用\ 转义 echo "\""; echo '"'; //针对双引号,单引号有了避免了转义的操作。
respectively. So, through the above explanation, we can conclude that when we just output a string and there is nothing that needs to be explained, we You should use '' single quotes directly. This can omit an explanation process. Although there is no obvious improvement in the running speed, in principle the running efficiency is higher than using double quotes. When the content we output needs to be interpreted, we can. You can use "" double quotes. For example, the output of echo "aaaaaaabbcc"
A problem occurred here. Please see the code
Php code
$res = 'xxx'; //第一步 echo "aaa$resbbbb"; //第二步 //这样,php在解析过程中,会当作$resbbbb 是一个变量,自然就会报错, //如何规避这样的情况呢。 echo “aaa{$res}bbbb”; //第三步
The author actually wants the variable $res. What is obtained is $resbbbb ,
Because zend uses regular matching when parsing. I don’t know that you only want res. As long as the variable is a character (_ character and number, and does not start with a number), Then the system regularization will always match. If there are spaces, the variable identification will naturally end here. (Of course, the spaces here only need to be characters in the variable naming specification)
echo "aaa$res bbbb. "; This sentence will run normally, but there is an extra space in the output string.
The author doesn't want the extra space?
Then use the code in the third step echo "aaa{$res}bbbb" ;
Written like this, when zend parses, it will also find two curly brackets. The characters in {} will be matched using normal double quotes, so that $res will be found directly. The curly braces will not be output
If you say What about {aaa $res} in the curly braces? That is: echo "aaa{ggg $res}bbbb"; That is, there are more than just variables in the curly braces or not variables. At this time, the system will re-judge the entire string. Double quotation marks are used to parse and find variables. At this time, the "}" after res does not belong to the variable naming.
So the above output will be: aaa{ggg xxx}bbbb
Summary: When the string and variable are concatenated and output. . The more efficient way is to enclose variables with {{}. Of course, there should be no non-variables in {}, otherwise it will be slower than using "" directly.