Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Adding in php question

Question posted by: TheServant (Needs Regular Fix) on May 18th, 2008 05:25 AM
I have the following code which is part of a for loop. There are 2 arrays previously defined ($var, $bar):
Code: ( text )
  1. echo ( "<tr><td><input name=\"".$var[$i]."_guild_upgrade\" type=\"submit\" value=\"Level ".++$bar[$i]."\" /></td></tr>" ) ;

That works 100%. However that is not what I tired first. I was tring to do this:
Code: ( text )
  1. echo ( "<tr><td><input name=\"".$var[$i]."_guild_upgrade\" type=\"submit\" value=\"Level ".$bar[$i]+1."\" /></td></tr>" ) ;

This was coming up with a parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING?
Does anyone know why I can't use +1 instead of ++?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
hsriat's Avatar
hsriat
Expert
1,427 Posts
May 18th, 2008
06:49 AM
#2

Re: Adding in php question
Enclose the adding thing in brackets.

($bar[$i]+1)

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 18th, 2008
11:24 AM
#3

Re: Adding in php question
That's it! Thanks for that. I will continue using the ++ though!

Reply
hsriat's Avatar
hsriat
Expert
1,427 Posts
May 18th, 2008
11:46 AM
#4

Re: Adding in php question
Quote:
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.

Reply
Atli's Avatar
Atli
Moderator
1,917 Posts
May 18th, 2008
02:42 PM
#5

Re: Adding in php question
It should be noted that by using ++ instead of +1 you are in fact changing the value of your array element.

Consider this:
Code: ( text )
  1. $value = 1;
  2. echo "Value + 1 = ". ++$value ."<br />";
  3. echo $value; # Prints 2

Code: ( text )
  1. $value = 1;
  2. echo "Value + 1 = ". ($value + 1) ."<br />";
  3. echo $value; # Prints 1

Reply
markusn00b's Avatar
markusn00b
Expert
1,812 Posts
May 18th, 2008
04:32 PM
#6

Re: Adding in php question
Quote:
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: ( text )
  1. $value = 1;
  2. echo "Value + 1 = ". ++$value ."<br />";
  3. echo $value; # Prints 2

Code: ( text )
  1. $value = 1;
  2. echo "Value + 1 = ". ($value + 1) ."<br />";
  3. echo $value; # Prints 1


So $value will remain at 2.

Suarve.

Reply
Atli's Avatar
Atli
Moderator
1,917 Posts
May 18th, 2008
05:21 PM
#7

Re: Adding in php question
Quote:
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: ( text )
  1. $someArray = array(1, 2, 4, 8, 16, 32, 64);
  2. for($i = 0; $i < count($someArray); $i++) {
  3.   echo "Element ". ++$i ." = ". $someArray[$i] ."<br />";
  4. }

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).

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 18th, 2008
10:03 PM
#8

Re: Adding in php question
Hmm, that's good to know, it works in my case but I will have to keep that in mind!

Reply
Reply
Not the answer you were looking for? Post your question . . .
174,849 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top PHP Forum Contributors