Home  >  Article  >  Backend Development  >  In-depth analysis of the difference between commas and periods in PHP_PHP Tutorial

In-depth analysis of the difference between commas and periods in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:00:27738browse

Copy code The code is as follows:

echo 'abc'.'def'; //Use dots to connect strings
echo 'abc','def'; //Use commas to connect strings

Then let's give some examples below to understand their previous differences.
Copy the code The code is as follows:

echo '1+5=' . 1+5;

Look at the output above. The result is 6.. instead of 1+5=6. Is it a bit magical?
What’s even more amazing is that you look at the example below.
Copy code The code is as follows:

echo "1+5=" . 5+1; //Output 2

The result is very strange. We see that we combine 5 and 1 Change the position. The result becomes 2.
Why is this? Is there no commutative property in addition in PHP? Of course not...
Let's not think about why. If I put the dot above Try changing it to a comma.
Copy the code The code is as follows:

echo '1+5=' , 5+1; //Output 1+5=6
echo '1+5=' , 1+5; //Output 1+5=6

As you can see, we can only use commas Get the expected results.
Then why doesn’t the period work? Why does the comma work?
Copy the code The code is as follows:

echo ('1+5' . 5)+1; //Output 2

After we add parentheses to the previous one, the result is the same. It proves that PHP is first Concatenate the strings and then perform the addition calculation. Do it in the direction from left to right.

So good. Since the strings are connected first, then it should be "1+55". Then use This string adds 1. Then why does it output 2?
This is related to the mechanism of converting strings into numbers in PHP. Let’s look at the following example
Copy code 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 character The string is forced to be converted into a number. PHP will search for the beginning of the string. If the beginning is a number, convert it. If not, it will directly return 0.

Return to the 1+55 just now. Since this string is 1+55. So it should be 1 after forced type conversion. Add 1 to this. Of course it is 2.
In order to prove our conjecture, let’s verify it.
Copy code 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 proved that our assumption was correct.
So why does the above problem disappear when using commas?
The manual says. Using commas is multiple parameters.
means multiple parameters. In other words,
separated by commas is equivalent to N parameters. In other words, use echo as a function.
In this case .echo will calculate each parameter first and then connect it and output it. So if we use commas, the above problem will not exist

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328078.htmlTechArticleCopy the code as follows: echo 'abc'.'def'; //Use dots to connect the string echo ' abc','def'; //Use commas to connect strings. Let's give some examples below to understand their previous divisions...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn