Advertisement:

Author Topic: PHP: echo- comma or dot?  (Read 4718 times)

Offline Tristan Perry

  • SMF Hero
  • ******
  • Posts: 2,498
  • Gender: Male
    • Tristan Perry
PHP: echo- comma or dot?
« on: February 09, 2005, 03:47:48 PM »
Hello,
 Is there any difference in using either a comma , or a dot . in a PHP statement. What I mean is, is doing what is posted below the same PHP-wise?

Code: [Select]
<?php
$var 
= &#39;Test&#39;;

echo &#39;This is a &#39;.$test.&#39; code.&#39;;
?>
Code: [Select]
<?php
$var 
= &#39;Test&#39;;

echo &#39;This is a &#39;,$test,&#39; code.&#39;;
?>

I use a dot, but I've seen many scripts (Including SMF) use a comma and so am wondering this.
 Thanks,
 Tau Online

Offline Jack.R.Abbit™

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,283
  • Gender: Male
Re: PHP: echo- comma or dot?
« Reply #1 on: February 09, 2005, 04:29:40 PM »
you trying to start some sort of fight?  LOL
I'm sure there will be plenty of people after me that can post some benchmark stats proving that one is better than the other... or vise-versa.  But here's my short answer with no benchmark stats to back it up.  For the most part they are the same.  I believe you will see both in the SMF code.  I'm not too sure why one would be used in place and the other used elsewhere.  I have always used the period... probably always will.  Some times after makeing a mod, I have to force myself to go back and change them all to commas just to fit in.  :)

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: PHP: echo- comma or dot?
« Reply #2 on: February 09, 2005, 04:57:12 PM »
There is a difference.  One way outputs each parameter, one by one, without sticking them together and eating a lot more memory.  The other uses more memory, takes more time to convert for echo, and is generally worse.

I've not seen many scripts that use echo with commas.

-[Unknown]

Offline Anguz

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 3,430
  • Gender: Male
    • cristianlavaque.com
Re: PHP: echo- comma or dot?
« Reply #3 on: February 09, 2005, 05:04:23 PM »
Tau, they aren't the same. The dots concatenate the parts and go as one echo, the commas result in a new echo for each part. The dots are a bit slower because it has to concatenate before echoing, multiple echos are faster, specially when you're buffering the output like SMF does.

Jack, I think SMF only uses commas.

BTW, Tau, use spaces: one before and after of dots when concatenating, and only one after when using commas in echos.

Code: [Select]
echo 'This is a ' . $test . ' code.';
echo 'This is a ', $test, ' code.';

Some reading that may help:
http://phplens.com/lens/php-book/optimizing-debugging-php.php
http://www.simplemachines.org/community/index.php?topic=7110.0

HIH.

Edit: [Unknown] was faster than me typing the reply. ^^
Cristián Lávaque http://cristianlavaque.com

Offline Peter Duggan

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 4,656
  • Gender: Male
    • Peat Stack or Pete's Tack?
Re: PHP: echo- comma or dot?
« Reply #4 on: February 09, 2005, 06:47:32 PM »
Code: [Select]
<?php
$var 
= &#39;Test&#39;;

echo &#39;This is a &#39;.$test.&#39; code.&#39;;
?>
Code: [Select]
<?php
$var 
= &#39;Test&#39;;

echo &#39;This is a &#39;,$test,&#39; code.&#39;;
?>

Totally irrelevant to your original question, but haven't you used $test where you meant $var there?

Offline Jack.R.Abbit™

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,283
  • Gender: Male
Re: PHP: echo- comma or dot?
« Reply #5 on: February 10, 2005, 01:50:29 AM »
Jack, I think SMF only uses commas.
Correct.  SMF uses dots in only a few files and for different reasons.  I must have been thinkiing of YSE or early SMF code.

But I'm disappointed that no one posted any benchmark stats showing that one is faster than the other.  :)

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: PHP: echo- comma or dot?
« Reply #6 on: February 10, 2005, 01:55:01 AM »
I could but I'm tired of doing it.  I could probably post several links as backup.

-[Unknown]

Offline Tristan Perry

  • SMF Hero
  • ******
  • Posts: 2,498
  • Gender: Male
    • Tristan Perry
Re: PHP: echo- comma or dot?
« Reply #7 on: February 10, 2005, 12:23:43 PM »
Ah right, thanks for the replies all, they've been helpful  :)