PHP and Echo
As any beginner PHP could tell you, the echo function outputs data to the screen (or command line depending on how you're using PHP). Some, however, may not know that PHP's "echo()" isn't really a function at all. From PHP.net:
"echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses."
So, instead of traditionally coding this...
echo("The MD5 of $text is: ".md5($text)."!");
...you can actually do this...
echo "The MD5 of $text is: ",md5($text),'!';
In fact, separating strings with commas is faster than concatenation! With a name like echo, it should be a bit crazy, right?
![Introducing MooTools Templated]()
One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating:
new Element Madness
The first way to create UI-driven...
![Camera and Video Control with HTML5]()
Client-side APIs on mobile and desktop devices are quickly providing the same APIs. Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop. One of those APIs is the getUserMedia API...
![Create WordPress Page Templates with Custom Queries]()
One of my main goals with the redesign was to make it easier for visitors to find the information that was most popular on my site. Not to my surprise, posts about MooTools, jQuery, and CSS were at the top of the list. What...
![MooTools OpenLinks Class – Updated]()
A long time back I coded a MooTools class called OpenLinks. The class is quite useful but the code...sucks. I've gotten much better with MooTools over the past years so I thought I'd go back and update the class to be better, faster...
Whoa the comma thing is a mindfreak. That saves some concatenated html output hell.
“In fact, separating strings with commas is faster than concatenation!”
I’m not sure what you mean by this? How is it faster?
I don’t think I have ever used parentheses with the echo. It’s weird to me to see it done like that.
@deef: It’s faster because PHP doesn’t need to continuously concatenate string upon string — commas allow for “Boom! Boom! Boom! Boom!” instead of “……….BOOM!”.
@mark: I was high-horse about it for a while. To me it was a function so “echo()” was “best form.” Foolish, I was.
I ran a quick test to see if I would notice some speed differences using the commas, noticed about 10% output speed increase in the test script. Funny… didn’t kow about it. Thank you!
Well, I always use echo; I didn’t actually know you could do echo(), but no matter. Not a function? Strange.
I read about the comma versus dot thing on an optimization post. According to this guy’s tests is was nowhere near 10%, but commas were still faster nonetheless.
http://making-the-web.com/2007/08/24/tips-for-faster-php-scripts/
I always hated the concatenation! but didnt know that commas do it faster.
Thanks
Find an interesting PHP-Benchmark.
Somewhere down the page, there is also an echo-Benchmark.
Seems like commas are not allways faster.
http://www.phpbench.com/
Find => Found
allways => always
It was to early in the morning, sorry! :-)
http://www.phpbench.com/
The author only compares echo with commas/periods using either all string or all variable. I’d be interested in seeing a comparison using some combination of both variables and commas.. something like
$a = ‘bbbbbb’;
echo ‘aaaaaa’,$b,’aaaaaa’,$b;
vs
echo ‘aaaaaa’.$b.’aaaaaa’.$b;