In php programming, the difference between using commas for echo and using dots is that using commas for echo

Source: Internet
Author: User

In php programming, the difference between using commas for echo and using dots is that using commas for echo

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 individually 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.

Articles you may be interested in:
  • Php echo output string functions
  • PHP learning output string (echo, print, printf, print_r and var_dump)
  • Comparison of echo, print, print_r, printf, sprintf, and var_dump output by php
  • Differences and usage between PHP echo, print, printf, and sprintf Functions
  • How to Use php function recursion and difference between return and echo
  • Differences between echo and print in PHP
  • Differences between echo, print_r and var_dump in PHP
  • Differences between print (), print_r (), and echo () in php

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.