The difference between Echo's comma and dot-number connection in PHP programming

Source: Internet
Author: User
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;  

can be seen. Only by using commas can we get the expected results.

So why not order it? Why would a comma be OK?

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 individually as multiple arguments or//concatenated together and passed as a sing Le 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.

The above describes the PHP programming echo with the comma and the point of connection between the difference, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.