Differences between using commas for echo and using dots in php programming

Source: Internet
Author: User
: This article mainly introduces the difference between using commas (,) for echo in php programming and using dot numbers for connection. For more information about PHP tutorials, see. The echo string is better than the. connection. The reason is not mentioned. let's take a look at the following two sentences:

<? Php // is more time-saving than comma? Echo '1 + 5 = '. 1 + 5; echo '1 + 5 ='. 5 + 1;

What is 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 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.

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?

Echo ('1 + 5'. 5) + 1; // output 2

After we add a bracket to the front, the result is the same.

It turns out that PHP is first connected to the string before addition calculation. it is performed from left to right.

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.

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 it starts with a number, it will be converted.

If not, 0 is 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.

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 the parameter. Therefore, the above problem will not exist if we use commas :)

Php echo manual

<?php// Strings can either be passed inpidually as multiple arguments or// concatenated together and passed as a single argumentecho 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";

For the sake of speed, you can simply understand it. it is first spliced in echo, although the number of commas represents the number of echo calls (this can be understood for the moment ).
However, the splicing speed is smaller than the echo speed.
For more information, see VLD. Is the map of @ tywei


The above has more CONCAT than below, and the following has more echo than above.

The above introduces the difference between using commas (,) for echo in php programming and using dots (,) for connection, including some content. I hope my friends who are interested in PHP tutorials will be 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.