PHP faster using commas quick tip

  • baligena
  • Novice
  • Novice
  • No Avatar
  • Joined: Apr 15, 2011
  • Posts: 25
  • Status: Offline

Post April 17th, 2011, 9:56 am

In PHP you can concatenate with a comma instead of a period when using echo. Commas make you script faster.

example:

the fast:
echo $var . $var2;

the furious
echo $var, $var2;
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post April 17th, 2011, 9:56 am

  • natas
  • PHP Ninja
  • Proficient
  • No Avatar
  • Joined: Mar 28, 2009
  • Posts: 305
  • Loc: AFK
  • Status: Offline

Post April 17th, 2011, 10:35 am

I'm just curious as to why and how commas make the script faster. Could you please elaborate?
Custom Web Design
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 3415
  • Loc: Richland, WA
  • Status: Offline

Post April 17th, 2011, 12:22 pm

My guess is one combines them (the variables) and the other runs the command twice.
#define NULL (::rand() % 2)
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6134
  • Loc: Seattle, WA
  • Status: Offline

Post April 18th, 2011, 10:44 am

Close.

echo() can take one more more arguments to print. Using a comma simply passes more arguments to echo().

A dot causes the strings to first be concatenated, which is a fairly expensive operation, before being passed as a single argument to echo().

In either case, the function is still only called once.
The Beer Monocle. Classy.
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Joined: Apr 28, 2011
  • Posts: 214
  • Loc: Sweden
  • Status: Offline

Post April 30th, 2011, 7:46 am

Are there any (known) downside with the use of commas?

For instance could the output become different (than what I'm used to) when echoing with commas instead of dots?
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13458
  • Loc: Florida
  • Status: Offline

Post April 30th, 2011, 8:15 am

It's possible, but the only time it's going to happen is pretty much when you're trying to break something.

Code: [ Select ]
<?php

class looker
{
    public function __toString()
    {
        return '[' . ob_get_contents() . '] [appended]';
    }
}

ob_start();

echo 'one', new looker(), PHP_EOL;

ob_flush();

echo 'two' . new looker() . PHP_EOL;

?>
  1. <?php
  2. class looker
  3. {
  4.     public function __toString()
  5.     {
  6.         return '[' . ob_get_contents() . '] [appended]';
  7.     }
  8. }
  9. ob_start();
  10. echo 'one', new looker(), PHP_EOL;
  11. ob_flush();
  12. echo 'two' . new looker() . PHP_EOL;
  13. ?>


Code: [ Select ]
joebert@frankenstien:~/Desktop$ php -f _.php
one[one] [appended]
two[] [appended]
  1. joebert@frankenstien:~/Desktop$ php -f _.php
  2. one[one] [appended]
  3. two[] [appended]


Try changing the periods in the last line to commas, and see what happens to the output.
Strong with this one, the sudo is.
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Joined: Apr 28, 2011
  • Posts: 214
  • Loc: Sweden
  • Status: Offline

Post May 1st, 2011, 2:33 pm

Thanks for explaining joebert. I don't fully understand that but its not because you explains in a hard to understand way.

But there is some code there that I have yet to play with and have little clue what it does (ob_get_contents()), ([appended]) and (ob_start()). I will look back at this when I have done some more research and hopefully by then I will understand your example better!
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13458
  • Loc: Florida
  • Status: Offline

Post May 1st, 2011, 3:11 pm

What that code is doing, is looking at the output buffer while generating the string that the class should return.

_toString is what's called a "magic method" in PHP classes. Basically, any time an instance of a class, for instance when it's passed to echo, needs to return a string for whatever reason, _toString is a method you can define in the class definition as a way to determine how your class will return a string.

For instance, if you have a class named "person" with two attributres, "first_name" and "last_name".

Code: [ Select ]
class
{
  public $first_name, $last_name;

  public function __construct($first, $last)
  {
   $this->first_name = $first;
   $this->last_name = $last;
  }
}
  1. class
  2. {
  3.   public $first_name, $last_name;
  4.   public function __construct($first, $last)
  5.   {
  6.    $this->first_name = $first;
  7.    $this->last_name = $last;
  8.   }
  9. }


You could setup something like this to get a "person"'s full name.

Code: [ Select ]
$me = new person('joe', 'bert');
echo $me->first_name . ' ' . $me->last_name;
  1. $me = new person('joe', 'bert');
  2. echo $me->first_name . ' ' . $me->last_name;


Or, you could setup a _toString method, which PHP will know to call automatically in order to generate the string when you do something such as

Code: [ Select ]
echo new person('joe', 'bert');


For instance

Code: [ Select ]
class
{
  public $first_name, $last_name;

  public function __construct($first, $last)
  {
   $this->first_name = $first;
   $this->last_name = $last;
  }

  public function __toString()
  {
   return $this->first_name . ' ' . $this->last_name;
  }
}

$me = new person('joe', 'bert');
define('GREETING', 'Hello, my name is %s');

printf(GREETING, $me);
  1. class
  2. {
  3.   public $first_name, $last_name;
  4.   public function __construct($first, $last)
  5.   {
  6.    $this->first_name = $first;
  7.    $this->last_name = $last;
  8.   }
  9.   public function __toString()
  10.   {
  11.    return $this->first_name . ' ' . $this->last_name;
  12.   }
  13. }
  14. $me = new person('joe', 'bert');
  15. define('GREETING', 'Hello, my name is %s');
  16. printf(GREETING, $me);


Now realistically, chances are you're never going to have a class that looks at the output buffer before generating a string for output. The only time I could think it would even be reasonable (somewhat) is if you had a class that defined a part of the English language that looked at the previously printed output to see how it should print something.

For instance, if you had a keyword that could be singular or plural depending on what was printed before it, you could return whichever one would make more sense for the currently generated sentence. Now, in 7 years I've never seen anything like that done, the example I posted is actually the first time I've seen anything like this period. So, you're probably safe assuming it's almost never going to be something you need to worry about, :)
Strong with this one, the sudo is.
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Joined: Apr 28, 2011
  • Posts: 214
  • Loc: Sweden
  • Status: Offline

Post May 2nd, 2011, 10:40 am

You know what, I think I got it now after some reading! 8) Also I appreciate your well written code, it got me questioning my own code and realizing that I should use the word "public" instead of "var" when declaringpublic variable(s).

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 173 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.