This article uses a few specific examples to identify the difference between the Echo and the comma in PHP programming before connecting with a dot. The need for small partners can refer to the next, I hope to be helpful to everyone.
It mentions the Echo string, which is better than the connection. The reason is not to say, first look at the following two sentences
<?php//comma ratio. More time saving? Echo ' 1+5= '. 1+5; Echo ' 1+5= '. 5+1;
What was 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 this so? Is there no exchange law for addition in PHP? Of course not.
Let's not think about why. If I change the dot above to a comma, try it.
Echo ' 1+5= ', 5+1; Output 1+5=6 echo ' 1+5= ', 1+5; Output 1+5=6
can be seen. Only by using commas can we get the expected results.
So why not order it? Why would a comma be OK?
Echo (' 1+5 '. 5) +1; Output 2
We give the previous parentheses. The result is the same.
Prove that PHP is connected to the string before the addition calculation. In the direction from left to right.
So good. Since it is a string that is connected first. Then it should be "1+55". Then use this string to add 1. So why would it output 2?
This is related to the mechanism in PHP where strings become numbers. Let's take a look at the following example
echo (int) ' ABC1 '; Output 0echo (int) ' 1ABC '; Output 1echo (int) ' 2ABC '; Output 2echo (int) ' 22ABC '; Output 22
As we can see from the above example, if you cast a string into a number. PHP will search for the beginning of the string. If it starts with a number, it is converted.
If not, return directly to 0.
Go back to the 1+55. Since this string is 1+55, it should be 1 after forcing type conversion. On this basis add 1. Of course 2.
To prove our conjecture. Let's check it out.
Echo ' 5+1= '. 1+5; Output 10echo ' 5+1= '. 5+1; Output 6echo ' 1+5= '. 1+5; Output 6echo ' 1+5= '. 5+1; Output 2
The result proves that our vision is correct.
So why do you use commas without the above problem?
The manual says. A comma is the multiple parameters.
That is, multiple parameters. In other words.
A comma separates the equivalent of n arguments. That is to say echo is used as a function.
In that case. Echo evaluates each parameter first. Finally, the connection is then output. So we don't have the above problem with 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 ', ' is ', ' made ', ' with multiple parameters. ', CHR; Echo ' this '. ' String '. ' was '. ' Made '. ' with concatenation. ' "\ n";
As for why fast, can be simple to understand, with. Is the first stitching in Echo, although the number of commas represents the number of calls to echo (which can be understood so temporarily).
But the stitching speed is smaller than the echo speed.
If in-depth understanding, VLD such as. It's a picture of the @tywei God.
There is more than below a concat, below a more than above the echo.
Related recommendations:
PHP Syntax (Syntax)
PHP learns the route of the MVC framework
PHP namespace Usage detailed