adding to a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stijn Goris

    adding to a string

    hi all,

    I have a string $string

    I initialize the string:
    $string = "test string<br>";

    I want to add text to the string along my script but:
    $string += "more information<br> ";

    doesn't seem to work.

    Someone an help me?

    regards
    Stijn


  • zapf

    #2
    Re: adding to a string

    El Dijous 29 Gener 2004 16:49, Stijn Goris va escriure:

    Hi!
    [color=blue]
    > I want to add text to the string along my script but:
    > $string += "more information<br> ";
    >
    > doesn't seem to work.[/color]

    Try:
    $string .= "more information<br> ";

    Regards

    --

    En cap cap cap el que cap en aquet cap.

    Comment

    • Keith Bowes

      #3
      Re: adding to a string

      zapf wrote:[color=blue][color=green]
      >>$string += "more information<br> ";
      >>
      >>doesn't seem to work.[/color]
      >
      >
      > Try:
      > $string .= "more information<br> ";
      >[/color]

      Yeah, but why didn't they overload the + operator like other languages?
      Because for concatenation, we have ., which is traditionally used for
      object access. For that, we have the C++ object pointer operator, ->.
      Not that I'm complaining, but it seems to be an unnecessary deviation
      from the norm in such a fundamental area.

      Comment

      • Phil Roberts

        #4
        Re: adding to a string

        With total disregard for any kind of safety measures Keith Bowes
        <do.not@spam.me > leapt forth and uttered:
        [color=blue]
        > zapf wrote:[color=green][color=darkred]
        >>>$string += "more information<br> ";
        >>>
        >>>doesn't seem to work.[/color]
        >>
        >>
        >> Try:
        >> $string .= "more information<br> ";
        >>[/color]
        >
        > Yeah, but why didn't they overload the + operator like other
        > languages?
        > Because for concatenation, we have ., which is traditionally
        > used for
        > object access. For that, we have the C++ object pointer
        > operator, ->. Not that I'm complaining, but it seems to be an
        > unnecessary deviation from the norm in such a fundamental area.
        >
        >[/color]

        Becuase thats the way PHP works. The . operator for concatenation
        was borrowed from Perl, as was the -> operator (although it
        originated in C++)

        In PHP += Is used in numeric calculations, I think $i += 2 is the
        same as $i = $i + 2.

        --
        Phil Roberts | Nobody In Particular | http://www.flatnet.net/

        Comment

        • Pedro Graca

          #5
          Re: adding to a string

          Keith Bowes wrote:[color=blue]
          > Yeah, but why didn't they overload the + operator like other languages?
          > Because for concatenation, we have ., which is traditionally used for
          > object access. For that, we have the C++ object pointer operator, ->.
          > Not that I'm complaining, but it seems to be an unnecessary deviation
          > from the norm in such a fundamental area.[/color]

          so that you can do:

          <?php
          $n = 3;
          $s = '6';

          echo $n + $s; // 9
          echo $n . $s; // 36
          echo $s + $n; // 9
          echo $s . $n; // 63
          ?>


          compare with my feeble attempt at C++

          #include <iostream.h>

          int main(void) {
          int n=3;
          char* s="6";
          // I don't know how to display 9 or 36
          // as the result of an operation over n
          // and s now
          cout << n << s << "\n"; // ok for 36 :-)
          return 0;
          }
          --
          --= my mail box only accepts =--
          --= Content-Type: text/plain =--
          --= Size below 10001 bytes =--

          Comment

          • CountScubula

            #6
            Re: adding to a string

            it also helps if you are looking at some code that was written a while back,
            and you are midstream and see:

            $var .= $var2;

            you know this is string concatenation not a math addition.

            if you were to see:

            $var += $var2

            you would have no idea if it is $var = $var + $var2 or if it is $var =
            "$var$var2"
            this is another reason it is serperate.

            just my $.02

            --
            Mike Bradley
            http://www.gzentools.com -- free online php tools
            "Keith Bowes" <do.not@spam.me > wrote in message
            news:nxbSb.1565 3$dF4.11829@fe0 2.usenetserver. com...[color=blue]
            > zapf wrote:[color=green][color=darkred]
            > >>$string += "more information<br> ";
            > >>
            > >>doesn't seem to work.[/color]
            > >
            > >
            > > Try:
            > > $string .= "more information<br> ";
            > >[/color]
            >
            > Yeah, but why didn't they overload the + operator like other languages?
            > Because for concatenation, we have ., which is traditionally used for
            > object access. For that, we have the C++ object pointer operator, ->.
            > Not that I'm complaining, but it seems to be an unnecessary deviation
            > from the norm in such a fundamental area.
            >[/color]


            Comment

            • Andy Hassall

              #7
              Re: adding to a string

              On Thu, 29 Jan 2004 12:35:45 -0500, Keith Bowes <do.not@spam.me > wrote:
              [color=blue][color=green][color=darkred]
              >>>$string += "more information<br> ";
              >>>
              >>>doesn't seem to work.[/color]
              >>
              >> Try:
              >> $string .= "more information<br> ";[/color]
              >
              >Yeah, but why didn't they overload the + operator like other languages?[/color]

              It's a matter of opinion as to whether this:

              <pre>
              <?php
              print "1" + "2";
              print "\n";
              print "1" . "2";
              ?>
              </pre>

              Should output:

              3
              12

              (which it does)

              Or:

              12
              12

              Both are reasonable from different points of view; PHP went with the first.
              Given that numeric values often come in as strings from $_GET/$_POST, always
              doing arithmetic for '+', and having concatenation as a separate operator
              probably makes more sense in the majority of cases.

              --
              Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
              <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

              Comment

              Working...