PHP faster using commas quick tip

  • baligena
  • Novice
  • Novice
  • baligena
  • Posts: 25

Post 3+ Months Ago

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;
  • natas
  • PHP Ninja
  • Proficient
  • natas
  • Posts: 308
  • Loc: AFK

Post 3+ Months Ago

I'm just curious as to why and how commas make the script faster. Could you please elaborate?
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

My guess is one combines them (the variables) and the other runs the command twice.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

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.
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

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
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

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.
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

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
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

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, :)
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

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 86 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.