tags:

views:

317

answers:

5

I just found that something like

echo $value , " contiue";

will work,but this not :

return $value , " contiue";

While "." works in both occasions.

+4  A: 

the . is the concatenation operator in PHP, for putting two strings together. The comma can be used for multiple inputs to echo.

GSto
So comma is concatenation operator only for echo?
Shore
at that respect it's not a concatination, it's just a 'list' of variables or stings to echo...
Nicky De Maeyer
No, comma is creating a list of expressions for echo to use, echo concatenates the list when it prints it on one line.
acrosman
But there is no bracket at all.
Shore
there doesn't need to be.
GSto
`echo` is not a function call, but a _language construct_. Language constructs in PHP can be called with or without parentheses.
Alex Barrett
+1  A: 

echo is actually a function (not really but let's say it is for argument) that takes any number of parameters and will concatenate them together. While return is not a function, but rather a keyword, that tells the function to return the value, and it is trying to interpret "," as some kind of operator. You should be using "." as the concatenation operator in the case when you are using the return statement.

Kibbee
+1  A: 

dot is for concatenation of variable or string this is why it works when you echo while concatening two string and it works when you return a concatenation of string in a method. But the comma won't concatenate and this is why the return statement won't work.

The echo is a function that can take multiple parameter. This is why the comma works :

void echo  ( string $arg1  [, string $...  ] )

Use the DOT for concatenation

Daok
But I'm using echo 'something',not echo('something') ,say,without brackets.
Shore
PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.
Daok
that's because echo is a keyword in PHP, in addition to being a function. you could write it as echo('something','something else') and it would also work fine.
GSto
no,not working syntax error.
Shore
Shore you should check the php.net website about echo. GSto and I are telling you exaclty what is written in the PHP documentation. And it works.
Daok
echo('something','something else') ;exit();Parse error: syntax error, unexpected ','
Shore
you cannot use parentheses when passing multiple parameters. Nobody uses it with parentheses anyway.
Juan Mendes
+3  A: 

return does only allow one single expression. But echo allows a list of expressions where each expression is separated by a comma. But note that since echo is not a function but a special language construct, wrapping the expression list in parenthesis is illegal.

Gumbo
Although not perfect,but nearest!
Shore
However echo does allow parentheses if there's only argument
Juan Mendes
+1  A: 

echo is a language construct (not a function) and can take multiple arguments, that’s why , works. using comma will be slightly even (but only some nanoseconds, nothing to worry about)

. is the concatenation operator (the glue) for strings

knittl