The difference _php technique of echo with comma and DOT connection in PHP programming

Source: Internet
Author: User
Tags php programming

It refers to 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= '. The result of 1+5 is 10, so the result is 6 and 2.

Why is it so. Is there no Exchange law for addition in PHP?
Let's not think about why. If I change the number above to a comma, try it.

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

You can see that. only by using commas can we get the expected results.

Then why not the order number? Why would commas do that?

Echo (' 1+5 '. 5) +1; Output 2 

We'll put parentheses in front. The result is the same.

Prove that PHP is the first connection string before the addition calculation. In the direction from left to right.

So good. Since it is the first concatenated string. Then it should be "1+55." and then use this string to add 1. Then why does it output 2?

This is related to the mechanism in which strings in PHP become numbers. Let's 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

We can see from the example above. If you cast a string to a number. PHP will search for the beginning of the string. If the beginning is a number, convert it.

If not, return directly to 0.

Back to the 1+55. Since this string is 1+55, it should be 1 after the coercion type conversion. On this basis, add 1. Of course it's 2.
To prove our conjecture. Let's verify.

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

As it turns out, our vision is right.
So why do we use commas without the above problems?

The handbook says, "comma is multiple parameters."

That is, multiple parameters. In other words.

Commas are separated by the equivalent of n arguments. That is, use echo as a function.

In that case. Echo calculates each parameter first. The final connection is made and the output is done. So we don't have the problem with commas:

PHP echo Manual

<?php
//Strings can either be passed individually as multiple arguments or
//concatenated together and passed As a single argument
echo ' This ', ' string ', ' is ', ' made ', ' with multiple parameters ', Chr (a);
Echo ' this '. ' String '. ' was '. ' Made '. ' with concatenation. ' "\ n";

As for why fast, can be simple to understand, with. is first spliced in Echo, although the number of commas represents the number of calls to echo (which can be understood temporarily).
But the stitching speed is less than the echo speed.
If you have a deeper understanding, VLD the following figure. It's the @tywei of the great God.


There was a concat below, and there was an echo above.

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.