echo $foo.$bar OR echo $foo,$bar
Posted: Fri Jun 30, 2006 9:19 am
What is the difference between and
There are a couple of differences that I know of.
Firstly is this one...
...which is an incentive to use commas.
And then there is this one...
...which is an incentive to use dot operators because you can easy and quickly change any echo to an assignment without having to go through and change all the operators you used.
Thing is I have a sneeky feeling that the comma is faster than the dot operator for output. Not being a Zend Engine programmer I can only spectulate but I have a feeling that this...
...is translated to...
...by the Zend Engine. Which may well be faster than this...
...which is probably translated to...
...by the Zend Engine.
Of course this is only a theory. I'm interested to know if I am correct.
Code: Select all
echo $foo.$barCode: Select all
echo $foo,$barFirstly is this one...
Code: Select all
class A {
public function __toString() {
return 'A';
}
}
$foo = new A();
// __toString not called with dot
echo '123 '.$foo; // outputs "123 Object id #1"
echo "\n";
// __toString called using commas
echo '123 ',$foo; // outputs "123 A";And then there is this one...
Code: Select all
$a = 'cerial';
$b = 'milk';
$c = 'occasionally';
$msg = 'I '. $c .' eat '. $a .' with'. $b; // I occasionally eat cerial with milk
$msg = 'I ', $c ,' eat ', $a ,' with', $b; // Parse error: syntax error, unexpected ','
// to make the line above work i'd need to change all those commas to dotsThing is I have a sneeky feeling that the comma is faster than the dot operator for output. Not being a Zend Engine programmer I can only spectulate but I have a feeling that this...
Code: Select all
echo $foo,$bar,$zim;Code: Select all
echo $foo;
echo $bar;
echo $zim;Code: Select all
echo $foo.$bar.$zim;Code: Select all
$tmp = $foo.$bar.$zim; // involves processing
echo $tmp;Of course this is only a theory. I'm interested to know if I am correct.