Discussion: The difference between a comma and a dot in PHP

Source: Internet
Author: User
With everyone to discuss, in PHP programming, the comma and dot difference of that point, there is a need for friends, may wish to refer to the next OH.

In PHP, the comma is faster than the dot number, what is the reason for geometry? This article will be for everyone.

Look at the code first:

 
      

Learn the difference between a comma and a dot in PHP by example.

Code: 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?

A magical example: Echo "1+5=". 5+1; Output 2 Replace position 5 and 1. The result becomes 2. Why is there no Exchange law in addition to PHP? Of course not. Let's not think about why. If I replace the dot above with a comma, try it. Echo ' 1+5= ', 5+1; Output 1+5=6echo ' 1+5= ', 1+5; Output 1+5=6

It can be seen that the expected results can be obtained only by using commas. So why not order the number? The comma is OK? Echo (' 1+5 '. 5) +1; Output 2 to the preceding parentheses. The result is the same. Prove that PHP is connected to the string before the addition calculation, according to the left-to-right direction.

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 of the string becoming a number in PHP.

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 can be seen from the example above: if you cast a string into a number, PHP will search for the beginning of the string, if it starts with a number, if not, it returns 0.

Back to the 1+55, since this string is 1+55, so the forced type conversion should be 1, on this basis add 1, of course 2. Verify that:

Echo ' 5+1= '. 1+5; Output echo ' 5+1= '. 5+1; Output 6 echo ' 1+5= '. 1+5; Output 6 echo ' 1+5= '. 5+1; Output 2

proved to be correct. So why do you use commas without the above problem? The manual says. Comma is multiple Parameters. That is, multiple parameters, that is, a comma separated by the equivalent of n parameters. That is, ECHO is used as a function. This way, echo evaluates each parameter first, and then outputs it after the connection, so there is no 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.