What is the best way to add two strings together?

前端 未结 12 523
自闭症患者
自闭症患者 2020-12-01 23:35

I read somewehere (I thought on codinghorror) that it is bad practice to add strings together as if they are numbers, since like numbers, strings cannot be changed. Thus, ad

12条回答
  •  感情败类
    2020-12-02 00:21

    As the others said, $str1 . $str2 is perfectly OK in most cases, except in (big) loops.
    Note that you overlook some solutions:

    $output = "$str1$str2";
    

    and for large number of strings, you can put them in an array, and use implode() to get a string out of them.

    Oh, and "adding strings" sounds bad, or at least ambiguous. In most languages, we prefer to speak of string concatenation.

提交回复
热议问题