In-depth analysis of the difference between comma and Dot in PHP

Source: Internet
Author: User
Most of you know that commas are faster than dots, but you just don't know why, or what is the difference between commas and dots. The following small series will introduce you in detail. if you need a friend, you can refer to it. The code is as follows:


Echo 'ABC'. 'Def '; // connect a string with a dot
Echo 'ABC', 'Def '; // use a comma to connect to a string


Here are some examples to identify their differences.

The code is as follows:


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.

The code is as follows:


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.

The code is as follows:


Echo '1 + 5 = ', 5 + 1; // output 1 + 5 = 6
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?

The code is as follows:


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.

The code is as follows:


Echo (int) 'abc1'; // output 0
Echo (int) '1abc'; // output 1
Echo (int) '2abc'; // output 2
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.

The code is 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


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.

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.