Php:echo the difference between commas and dot numbers

Source: Internet
Author: User

We all know that. In Echo, you can concatenate strings with commas. and tested. Such a connection string is faster than using the dot number directly.

Like what:
Echo ' abc '. Def '; Concatenate strings with dot numbers

Echo ' abc ', ' Def '; Concatenate strings with commas

Maybe a lot of people know that commas are faster than dots. But I don't know why. I don't know what the difference is.
So here are some examples. To identify the difference between them.
Echo ' 1+5= '. 1+5;
Look at the above. The result of the output is 6. Not 1+5=6. It's kind of magical, isn't it?
More amazing is the example you see below.
Echo ' 1+5= '. 5+1;
Output 2 The result is very strange. We saw that. We changed the position of 5 and 1. The result becomes 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; The 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.
ProvePHP is connected to the string before the addition calculation. In the direction of 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 0
echo (int) ' 1ABC '; Output 1
echo (int) ' 2ABC '; Output 2
echo (int) ' 22ABC '; Output 22
As 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 it is not, it will return 0 directly.
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 10
Echo ' 5+1= '. 5+1; Output 6
Echo ' 1+5= '. 1+5; Output 6
Echo ' 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 a problem with commas.

Php:echo the difference between commas and dot numbers

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.