In-depth analysis of the difference between comma and Dot in PHP. Copy the code as follows: echoabc. def; use the dot to connect to the echoabc and def strings; use a comma to connect to the strings. Here are some examples to identify their previous sections.
The code is as follows:
Echo 'ABC'. 'Def '; // connect a string with a dot
Echo 'ABC', 'Def '; // use a comma to connect to a string
Here are some examples to identify their differences.
The code is as follows:
Echo '1 + 5 = '. 1 + 5;
Look at the above. the output result is 6 .. instead of 1 + 5 = 6. is it amazing?
What's more amazing is that you can refer to the following example.
The code is as follows:
Echo "1 + 5 =". 5 + 1; // output 2
The result is very strange. we can see that we change the positions 5 and 1. The result is changed to 2.
Why is that? isn't there any exchange law in PHP? Of course not ..
Let's skip the question. if I replace the dot with a comma, try again.
The code is as follows:
Echo '1 + 5 = ', 5 + 1; // output 1 + 5 = 6
Echo '1 + 5 = ', 1 + 5; // output 1 + 5 = 6
We can see that only the comma can be used to obtain expected results.
So why can't I do it by the dot? Why is it just a comma?
The code is as follows:
Echo ('1 + 5'. 5) + 1; // output 2
After adding a bracket, we get the same result. it proves that PHP first connects to the string and then performs addition calculation. The result is in the left-to-right direction.
Well, since it is a first-connected string, it should be "1 + 55". then we can use this string to add 1. Then why will we output 2?
This mechanism is related to the conversion of strings into numbers in PHP. let's take a look at the following example.
The code is as follows:
Echo (int) 'abc1'; // output 0
Echo (int) '1abc'; // output 1
Echo (int) '2abc'; // output 2
Echo (int) '22abc'; // output 22
From the above example, we can see that if a string is forcibly converted into a number, PHP will search for the start of the string. if the start is a number, it will be converted. if not, 0 will be returned directly.
Return to 1 + 55. Since this string is 1 + 55, it should be 1 after forced type conversion. add 1 on this basis. of course it is 2.
To prove our conjecture, let's verify it.
The code is as follows:
Echo '5 + 1 = '. 1 + 5; // output 10
Echo '5 + 1 = '. 5 + 1; // output 6
Echo '1 + 5 = '. 1 + 5; // output 6
Echo '1 + 5 = '. 5 + 1; // output 2
The results prove that our ideas are correct.
So why is there no problem with using commas?
As mentioned in the manual, the comma is multiple parameters.
That is, multiple parameters. In other words.
Comma-separated values are equivalent to N parameters, that is, echo is used as a function.
In this case,. echo will calculate each parameter first, and then connect and output it. Therefore, the above problem will not exist if we use commas.
The http://www.bkjia.com/PHPjc/328078.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/328078.htmlTechArticle code is as follows: echo 'ABC '. 'def '; // use the dot to connect to the string echo 'ABC' and 'Def'; // use a comma to connect to the string. Here are some examples. to identify their previous zone...