Web Analytics Made Easy -
StatCounter Does PHP Use "+=" to Append/Concat String? - CodingForum


No announcement yet.

Does PHP Use "+=" to Append/Concat String?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Does PHP Use "+=" to Append/Concat String?

    If I want to append/concat multiple strings to a string in PHP, can I use "+=" and "+"? It seesms that it doesn't work. What's the most efficient/best way to do it in PHP?


    string1 += string2 + string3;

    Many thanks.
    Last edited by dealmaker; Aug 1, 2005, 03:05 AM.

  • #2
    The period is the concatenation operator in PHP. I think .= works too.


    • #3
      $a .= $b is the same as $a = $a.$b I think.


      • #4
        That is correct. The use of $a .= $b is by far an extremely useful method.
        As well, though I haven't put it to the test, $a += $b may very well work to be the same should the data be in string format. If it is not:
        $a = "0";
        $a += "1 more to add";
        // $a now equals 1.
        May not turn out the same as php will attempt to evaluate it.
        However, this may work (untested)
        $a = '0';
        $a += '1 more to add';
        // will $a equal '01 more to add'? Hmm, I don't want to test it

        Nope, php.net says it won't work
        Last edited by Fou-Lu; Aug 1, 2005, 11:27 AM.
        PHP Code:
        header('HTTP/1.1 420 Enhance Your Calm'); 
        Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


        • #5
          PHP knows that you can add only numbers (INT or FLOAT) so it'll change both strings to INT or FLOAT, add the values and return either an INT or FLOAT.
          So in both cases it would return INT 1.
          I'm not sure if this was any help, but I hope it didn't make you stupider.

          Experience is something you get just after you really need it.
          PHP Installation Guide Feedback welcome.