String concatenation vs array implode in PHP

后端 未结 5 1743
自闭症患者
自闭症患者 2021-01-12 18:54

Having used Java for a long time my standard method for creating long strings piece by piece was to add the elements to an array and then implode the array.

         


        
5条回答
  •  春和景丽
    2021-01-12 19:24

    To me, using an array implies that you're going to do something that can't be done with simple string concatenation. Like sorting, checking for uniqueness, etc. If you're not doing anything like that, then string concatenation will be easier to read in a year or two by someone who doesn't know the code. They won't have to wonder whether the array is going to be manipulated before imploded.

    That said, I take the imploded array approach when I need to build up a string with commas or " and " between words.

提交回复
热议问题