It is mentioned that using echo string is better than using .connection. Let’s not talk about the reasons first. Let’s take a look at the following two sentences
<p>What is the result? <br></p><p></p><pre class="brush:php;toolbar:false">1+5=6? 1+5=6? —————— 6? 2? —————— 6.6? 6.6? ——————
I can only say that the result of echo '5+1=' . 1+5; is 10, so the results are 6 and 2.
Why is this? Is there no commutative property in addition in PHP? ?Of course not..
Let’s not think about why. If I try to replace the period above with a comma.
echo '1+5=' , 5+1; //输出 1+5=6 echo '1+5=' , 1+5; //输出 1+5=6
It can be seen that only by using commas can we get the expected results.
Then why doesn’t the period work? Why does the comma work?
echo ('1+5' . 5)+1; //输出2
After we add parentheses to the previous one, the result is the same.
Prove that PHP concatenates strings first and then performs addition calculations Okay. Follow the direction from left to right.
So good. Since the string is connected first, it should be "1+55". Then use this string to add 1. Then why does it happen? What about output 2?
This is related to the mechanism of converting strings into numbers in PHP. Let’s take a look at the following example
echo (int)'abc1'; //输出0 echo (int)'1abc'; //输出1 echo (int)'2abc'; //输出2 echo (int)'22abc'; //输出22
From the above example we can see that if a string is forced to be converted into a number. PHP will search for the beginning of the string. If the beginning is a number, convert it.
If not, just return 0.
Return to the 1+55 just now. Since this string is 1+55. So after forced type conversion, it should be 1. Add 1 to this. Of course it is 2.
In order to prove our conjecture, let’s verify it.
echo '5+1=' . 1+5; //输出10 echo '5+1=' . 5+1; //输出6 echo '1+5=' . 1+5; //输出6 echo '1+5=' . 5+1; //输出2
The result proves that our assumption is correct .
So why does the above problem not occur when using commas?
The manual says. Using commas means multiple parameters.
That is to say, it is multiple parameters. In other words.
Comma separated It is equivalent to N parameters. In other words, use echo as a function.
In this case, echo will calculate each parameter first, and then connect it and output it. So if we use commas, the above problem will not exist. :)
php echo manual
<p>As for why it is fast, it can be easily understood. Use . to splice it into echo first, although the number of commas represents the number of echo calls (it can be understood this way for now). <br>But the splicing speed is slower than the echo speed. <br>If you understand it deeply, VLD is as shown below. It’s a picture of @tywei master </p><p><img src="http://image.codes51.com/Article/image/20160328/20160328172441_4817.jpg" baiduimageplusstatus="2" baiduimageplusrect="null" alt="The difference between using commas and dots to connect echo in PHP programming"><br></p><p>. The top has more CONCAT than the bottom, and the bottom has more echo than the top. </p> <p> The above introduces the difference between using commas and dots to connect echo in PHP programming, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials. </p> <p> </p>