PHP and Echo

By  on  

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?

Recent Features

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • By
    5 HTML5 APIs You Didn&#8217;t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

Incredible Demos

  • By
    dwProgressBar v2:  Stepping and Events

    dwProgressBar was a huge hit when it debuted. For those of you who didn't catch my first post, dwProgressBar is a MooTools 1.2-based progress bar which allows for as much flexibility as possible. Every piece of dwProgressBar can be controlled by CSS...

  • By
    Animated AJAX Record Deletion Using MooTools

    I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with MooTools JavaScript. The PHP - Content & Header The following snippet goes at the...

Discussion

  1. Niobe

    Whoa the comma thing is a mindfreak. That saves some concatenated html output hell.

  2. “In fact, separating strings with commas is faster than concatenation!”

    I’m not sure what you mean by this? How is it faster?

  3. I don’t think I have ever used parentheses with the echo. It’s weird to me to see it done like that.

  4. @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.

  5. 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!

  6. 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/

  7. I always hated the concatenation! but didnt know that commas do it faster.
    Thanks

  8. Thomas

    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/

  9. Thomas

    Find => Found
    allways => always

    It was to early in the morning, sorry! :-)

  10. 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;

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!