Deep analysis of the difference between commas and dots in PHP _php tips

Source: Internet
Author: User
Copy Code code as follows:

Echo ' abc '. ' Def '; connect string with dot number
Echo ' abc ', ' Def '; Connecting strings with commas

So let's give some examples here. To recognize the difference between them.
Copy Code code as follows:

Echo ' 1+5= '. 1+5;

Look at the top. The result of the output is 6. Rather than 1+5=6. Something magical?
What's even more magical is that you look at the following example.
Copy Code code as follows:

echo "1+5=".  5+1; Output 2

The result is very strange. We see, we're going to replace 5 and 1. The result becomes 2.
Why is it so. Is there no Exchange law for addition in PHP?
Let's not think about why. If I change the number above to a comma, try it.
Copy Code code as follows:

Echo ' 1+5= ', 5+1; Output 1+5=6
Echo ' 1+5= ', 1+5; Output 1+5=6

You can see that. only by using commas can we get the expected results.
Then why not the order number? Why would commas do that?
Copy Code code as follows:

Echo (' 1+5 '. 5) +1; Output 2

We'll put parentheses in front of it. 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 the first concatenated string. Then it should be "1+55." and then use this string to add 1. Then why does it output 2?
This is related to the mechanism in which strings in PHP become numbers. Let's look at the following example
Copy Code code as follows:

echo (int) ' ABC1 '; Output 0
echo (int) ' 1ABC '; Output 1
echo (int) ' 2ABC '; Output 2
echo (int) ' 22ABC '; Output 22

We can see from the example above. If you cast a string to a number. PHP will search for the beginning of the string. If the start is a number, convert it. If not, return directly to 0.

Back to the 1+55. Since this string is 1+55, it should be 1 after the coercion type conversion. On this basis, add 1. Of course it's 2.
To prove our conjecture. Let's verify.
Copy Code code 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

As it turns out, our vision is right.
So why do we use commas without the above problems?
The handbook says, "comma is multiple parameters."
That is, multiple parameters. In other words.
Commas are separated by the equivalent of n arguments. That is, use echo as a function.
In that case. Echo calculates each parameter first. The final connection is made and the output is done. So we don't have the problem with commas.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.