Jump to content

The Use of Commas and Periods with the echo() Construct


iwato

Recommended Posts

QUESTION: Would anyone like to comment on the use of commas and periods within the echo() construct? They appear to produce the same result, when all you want to do is display some output.COMMENT: Up until now, I have always used the concatenation "." operator to link script with the values of variables that I have wanted to appear in my strings. The code below shows that this can also be achieved with commas.

?><?phpclass foo {	function name() {		echo "My name is " , get_class($this) , "\n";	}}$bar = new foo();echo "Its name is " , get_class($bar) , "\n";$bar->name();?>

Roddy

Link to comment
Share on other sites

The thing is, as you're correctly pointing out, echo is a language construct. Many people don't realize what that means though, so they use it in a variety of odd ways that still produce the same result.The basic structure of echo is

echo expression [, expression [, ...]];

That is part of the construct's definition, in the same fashion that

for ([expression]; [expression]; [expression])

is the definition of the for construct, and

if (expression)

is the definition of an if construct.And "." is a concatenation operator. Like all operators in the strict definition of an "operator" (that would be "most" operators in the broader definition of an operator), the "." operator can be used in an expression.So, every time you're using a comma, you're separating one expression from another. Every time you're using ".", you're using concatenation within a single expression.Using "," will often be slighly more efficient, because string concatenation itself is not the most efficient process in PHP. With "," in echo, you're explicitly outputting one thing (well... put it in the output buffer to be more precise), and then another, whereas with ".", you're grouping things that eventually get outputted together.But the difference is like between single and double quotes - less than a milisecond per milion such echoes. So it's not something to seriously worry about. If you can, make it a habbit of using "," in echoes instead of string concatenation, but also keep in mind that outside of an echo, a comma is useless whereas "." works. On more than one occasion, I've had to move something that was previously echoed to a file or another context. In that situation, replacing all "," with "." is somewhat cumbersome, so nowadays, I only use "," if I'm really sure the thing at hand is likely to only get echoed.

Link to comment
Share on other sites

There's a series of benchmarks here to show the differences in string output:http://phpbench.com/The general rule looks like it's more efficient to use dot operators if you are concatenating literal strings, and if you're concatenating variables it seems better to use commas. Echo is also generally faster than print.

Link to comment
Share on other sites

The thing is, as you're correctly pointing out, echo is a language construct. Many people don't realize what that means though, so they use it in a variety of odd ways that still produce the same result.The basic structure of echo is
echo expression [, expression [, ...]];

That is part of the construct's definition, in the same fashion that

for ([expression]; [expression]; [expression])

is the definition of the for construct, and

if (expression)

is the definition of an if construct.And "." is a concatenation operator. Like all operators in the strict definition of an "operator" (that would be "most" operators in the broader definition of an operator), the "." operator can be used in an expression.So, every time you're using a comma, you're separating one expression from another. Every time you're using ".", you're using concatenation within a single expression.

Going into this question I knew that echo() could not be treated as a function, but really did not understand that it was a specially formatted statement. Your three examples were very useful, and I now feel much more comfortable about its use.Many thanks. Great job!Roddy
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...