This article uses several examples. to identify the difference between using commas (,) for echo in php programming and using a dot. for more information about echo strings, see echo strings. better connection. The reason is not mentioned. let's take a look at the following two sentences:
<? Php // is more time-saving than comma? Echo '1 + 5 = '. 1 + 5; echo '1 + 5 ='. 5 + 1;
What is the result?
1+5=6?1+5=6?——————6?2?——————6.6?6.6?——————
I can only say echo '5 + 1 = '. 1 + 5; the result is 10, so the result is 6 and 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.
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?
Echo ('1 + 5'. 5) + 1; // output 2
After we add a bracket to the front, the result is the same.
It turns out that PHP is first connected to the string before addition calculation. it is performed from left to right.
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.
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 it starts with a number, it will be converted.
If not, 0 is 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.
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 the parameter. Therefore, the above problem will not exist if we use commas :)
Php echo manual
<?php// Strings can either be passed inpidually as multiple arguments or// concatenated together and passed as a single argumentecho 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";
For the sake of speed, you can simply understand it. it is first spliced in echo, although the number of commas represents the number of echo calls (this can be understood for the moment ).
However, the splicing speed is smaller than the echo speed.
For more information, see VLD. Is the map of @ tywei
The above has more CONCAT than below, and the following has more echo than above.