Using PHP echo when the difference between the two. and, matter?

I recently noticed that there are two ways to print multiple statements in PHP.

echo $a.$b; // Echo $a and $b conjoined, and echo $a,$b; // Echo $a and echo $b. 

Is there any point in time when the difference between the two syntaxes matters?

+4
source share
5 answers

Really, no.

echo $a.$b first combines $a and $b in a new line , then passes it as an echo parameter that prints it.

echo $a,$b gives two parameters up to echo , which both will print.

The latter is a bit more efficient. Not as you usually noticed.

There is a difference in how it is evaluated. echo $a, $b is similar to the entry echo $a; echo $b; echo $a; echo $b; , two separate calls. $b will be evaluated after $a echo 'd. It may make a difference if your arguments are function calls that themselves echo something, but then again, in practice this should be irrelevant, as this is bad practice.

+6
source

There is a difference, which is good to notice. With the second syntax, the first parameter will be issued even if the second causes an error.

 <?php echo 1, error(); // This outputs 1, then it displays an error ?> 

While the first will not select the first part.

 <?php echo '1' . error(); // Only displays an error ?> 

When you separate your parameter with a comma, it will repeat them one by one. When you use the dot operator, it concatenates them into a string, and then it will echo.

+6
source

The difference does not matter in the vast majority of circumstances.

This can make a difference if the things you combine together are extremely large, where concatenation can be a little slower and can take up more memory. However, focusing on micro-optimization is usually the worst way to speed things up.

+4
source

programmatically:

  • combine 2 lines into 1 variable and send the string representation to the output buffer.

  • looping over two variables and sending the string representation to the output buffer.

two different methods for obtaining similar results. If you multiply these implementations by a significant number, you will see some differences.

+1
source

I asked for one member of PHP many years ago, and they said no. This is just another alternative syntax that allows you to have several ways to do something. There may be some advantages under the hood, but no one would notice a person.

0
source

Source: https://habr.com/ru/post/1315596/


All Articles