[PHP]-difference between comma and dot

Source: Internet
Author: User
For example:
1. Echo 'abc'. 'def '; // connect a string with a dot
2. Echo 'abc', 'def '; // use a comma to connect to a string
Many people may know that commas are faster than dots. But they do not know why. They do not know what the difference is.
Here are some examples to identify their differences.
1. Echo '1 + 5 = '. 1 + 5;
Look at the above. The output result is 6 .. instead of 1 + 5 = 6. Is it amazing?
What's more amazing is that you can refer to the following example.

1. Echo "1 + 5 =". 5 + 1; // output 2
The result is very strange. We can see that we change the positions 5 and 1. The result is changed to 2.
Why is that? Isn't there any exchange law in PHP? Of course not ..
Let's skip the question. If I replace the dot with a comma, try again.
1. Echo '1 + 5 = ', 5 + 1; // output 1 + 5 = 6
2. Echo '1 + 5 = ', 1 + 5; // output 1 + 5 = 6
We can see that only the comma can be used to obtain expected results.
So why can't I do it by the dot? Why is it just a comma?
1. Echo ('1 + 5'. 5) + 1; // output 2
After adding a bracket, we get the same result. It proves that PHP first connects to the string and then performs addition calculation. The result is in the left-to-right direction.
Well, since it is a first-connected string, it should be "1 + 55". Then we can use this string to add 1. Then why will we output 2?
This mechanism is related to the conversion of strings into numbers in PHP. Let's take a look at the following example.
1. Echo (INT) 'abc1'; // output 0
2. Echo (INT) '1abc'; // output 1
3. Echo (INT) '2abc'; // output 2
4. Echo (INT) '22abc'; // output 22
From the above example, we can see that if a string is forcibly converted into a number, PHP will search for the start of the string. If the start is a number, it will be converted. If not, 0 will be returned directly.
Return to 1 + 55. Since this string is 1 + 55, it should be 1 after forced type conversion. Add 1 on this basis. Of course it is 2.
To prove our conjecture, let's verify it.
1. Echo '5 + 1 = '. 1 + 5; // output 10
2. Echo '5 + 1 = '. 5 + 1; // output 6
3. Echo '1 + 5 = '. 1 + 5; // output 6
4. Echo '1 + 5 = '. 5 + 1; // output 2
The results prove that our ideas are correct.
So why is there no problem with using commas?
As mentioned in the manual, the comma is multiple parameters.
That is, multiple parameters. In other words.
Comma-separated values are equivalent to N parameters, that is, ECHO is used as a function.
In this case,. Echo will calculate each parameter first, and then connect and output it. Therefore, the above problem will not exist if we use commas.

Related Article

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.