Adding in php question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    Adding in php question

    I have the following code which is part of a for loop. There are 2 arrays previously defined ($var, $bar):
    [php]echo ( "<tr><td><i nput name=\"".$var[$i]."_guild_upgrad e\" type=\"submit\" value=\"Level ".++$bar[$i]."\" /></td></tr>" ) ;[/php]
    That works 100%. However that is not what I tired first. I was tring to do this:
    [php]echo ( "<tr><td><i nput name=\"".$var[$i]."_guild_upgrad e\" type=\"submit\" value=\"Level ".$bar[$i]+1."\" /></td></tr>" ) ;[/php]
    This was coming up with a parse error: syntax error, unexpected T_CONSTANT_ENCA PSED_STRING?
    Does anyone know why I can't use +1 instead of ++?
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Enclose the adding thing in brackets.

    ($bar[$i]+1)

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      That's it! Thanks for that. I will continue using the ++ though!

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        Originally posted by TheServant
        That's it! Thanks for that. I will continue using the ++ though!
        Yeh, that's better to use ++ instead of doing increment separately.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          It should be noted that by using ++ instead of +1 you are in fact changing the value of your array element.

          Consider this:
          [code=php]
          $value = 1;
          echo "Value + 1 = ". ++$value ."<br />";
          echo $value; # Prints 2
          [/code]
          [code=php]
          $value = 1;
          echo "Value + 1 = ". ($value + 1) ."<br />";
          echo $value; # Prints 1
          [/code]

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by Atli
            It should be noted that by using ++ instead of +1 you are in fact changing the value of your array element.

            Consider this:
            [code=php]
            $value = 1;
            echo "Value + 1 = ". ++$value ."<br />";
            echo $value; # Prints 2
            [/code]
            [code=php]
            $value = 1;
            echo "Value + 1 = ". ($value + 1) ."<br />";
            echo $value; # Prints 1
            [/code]
            So $value will remain at 2.

            Suarve.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by markusn00b
              So $value will remain at 2.

              Suarve.
              Yes, but if you are only trying to print the value + 1, incrementing it like this may cause problems.

              For example, if your trying to print all elements of an array:
              [code=php]
              $someArray = array(1, 2, 4, 8, 16, 32, 64);
              for($i = 0; $i < count($someArra y); $i++) {
              echo "Element ". ++$i ." = ". $someArray[$i] ."<br />";
              }
              [/code]
              This will skip every other element in the array and cause an undefined index error on the last loop.

              You would either have to skip the incrementation in the for loop or just do ($i + 1).

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                Hmm, that's good to know, it works in my case but I will have to keep that in mind!

                Comment

                Working...