Forums » Programming Discussion

Questions of a PHP noob

    • 20 posts
    June 20, 2016 6:13 AM EDT

    Although the answer is almost definately "it doesn't matter". When using echo in PHP is it better to write:

     

    echo $string1 . "-" . $string2;

    or

    echo $string1 , "-" , $string2;

     

    I understand that the first is doing string concatenation and the second is using the echo function's multi parameter version although does the echo funcion print multiple strings more efficiently (in terms of space and time)? As in would the echo function have to address additional space to store a concatenated form? If it doesn't and outputs sequentially then does that make the second variant slower as 3 sequential outputs may be slower than 1 concatenation where extra memory is addressed followed by only 1 output (the concatenated string)...

     

    I know this is a really small thing but I was just curious....

    • 14 posts
    June 20, 2016 11:18 AM EDT

    Galagatron,

    I can't address your memory issues, but somebody already addressed performance issues here:

    http://www.electrictoolbox.com/php-echo-commas-vs-concatenation/

    Here is my view of it. While comma "concatenation" is slightly faster (maybe 1-3 milliseconds over 100 operations), this is such an inconsequential drop in the bucket that the performance benefit isn't worth renouncing echo's more universal dot concatenation.

    TLDR; comma's are (slightly quicker) but choose dot concatenation for easier-to-understand code

    • 20 posts
    June 20, 2016 11:22 AM EDT

    Ah ok thanks MK!!! I had a feeling the commas might be faster due to the fact that it may not have to address new memory for the concatenation but wasn't sure. In all honesty though is commas that confusing? Like c'mon people...

    But as I expected not much of a difference.. Thanks for the link though! twas a good read

    • 14 posts
    June 20, 2016 11:38 AM EDT

    I'll be honest... I've got 4-5 years PHP experience and had never seen the commas. If I saw them in a piece of code they would seriously throw me off. Well they would, had I even noticed them!

    Before I read the rest of your post I didn't know what was different between your two pieces of code because I didn't notice the commas. They would definitely throw me off.

    • 4 posts
    July 3, 2016 2:27 AM EDT

    The '.' is the only concatenation symbol for PHP. http://php.net/manual/en/language.operators.string.php & http://php.net/manual/en/internals2.opcodes.concat.php.

     

    Here is a pretty good explaination of why the comma works. http://stackoverflow.com/a/1466433/3158829

    • 20 posts
    July 3, 2016 6:01 PM EDT

    Thanks dude, although I know what they do :) just wondering which one was faster for mass iterations..

    • 4 posts
    July 3, 2016 6:29 PM EDT

    Well the reason I was pasting the docs, is because you shouldn't use commas for concatenation period. It may not work in the future (since it isn't technically a feature). It is also awkward for other developers.

     

    So for the most part, the answer is, it doesn't really matter which is faster, don't use either. If you're iterating that big of a dataset, and it's too slow, you shouldn't be using PHP in the first place. Go or something would be a better option.

    • 20 posts
    July 4, 2016 2:03 AM EDT

    Ah ok thanks man :) sorry! I misunderstood the point of the post! Thanks for the help :)