473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Adding in php question

TheServant
1,168 Expert 1GB
I have the following code which is part of a for loop. There are 2 arrays previously defined ($var, $bar):
[php]echo ( "<tr><td><input name=\"".$var[$i]."_guild_upgrade\" 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><input name=\"".$var[$i]."_guild_upgrade\" type=\"submit\" value=\"Level ".$bar[$i]+1."\" /></td></tr>" ) ;[/php]
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 ++?
May 18 '08 #1
7 1061
hsriat
1,654 Expert 1GB
Enclose the adding thing in brackets.

($bar[$i]+1)
May 18 '08 #2
TheServant
1,168 Expert 1GB
That's it! Thanks for that. I will continue using the ++ though!
May 18 '08 #3
hsriat
1,654 Expert 1GB
That's it! Thanks for that. I will continue using the ++ though!
Yeh, that's better to use ++ instead of doing increment separately.
May 18 '08 #4
Atli
5,058 Expert 4TB
It should be noted that by using ++ instead of +1 you are in fact changing the value of your array element.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. $value = 1;
  2. echo "Value + 1 = ". ++$value ."<br />";
  3. echo $value; # Prints 2
  4.  
Expand|Select|Wrap|Line Numbers
  1. $value = 1;
  2. echo "Value + 1 = ". ($value + 1) ."<br />";
  3. echo $value; # Prints 1
  4.  
May 18 '08 #5
Markus
6,050 Expert 4TB
It should be noted that by using ++ instead of +1 you are in fact changing the value of your array element.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. $value = 1;
  2. echo "Value + 1 = ". ++$value ."<br />";
  3. echo $value; # Prints 2
  4.  
Expand|Select|Wrap|Line Numbers
  1. $value = 1;
  2. echo "Value + 1 = ". ($value + 1) ."<br />";
  3. echo $value; # Prints 1
  4.  
So $value will remain at 2.

Suarve.
May 18 '08 #6
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  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. }
  5.  
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).
May 18 '08 #7
TheServant
1,168 Expert 1GB
Hmm, that's good to know, it works in my case but I will have to keep that in mind!
May 18 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
1
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like...
9
by: Greg | last post by:
Binding Manager & dataset - won't add record I've got an untyped dataset with controls bound through code. The user can select a question number from a bound combobox, and the question number and...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
3
by: Ben Dewey | last post by:
Okay, so I have a base Page class called ArticlesPageBase. This base class has reads in an overridable ArticleId and Loads the data into a Panel object in the ContentPlaceHolder of the Master. ...
5
by: Kosmos | last post by:
I have traveled the world and the seven seas and I have yet to come up with an answer to this question.... So I'm adding an attachment to an email from access... The following is the code: ...
1
by: Preeti | last post by:
Hi all I am a fresher and have been given a requirement in VB.net I have to make an application in VB.net which will run as a system tray icon and will add one or two items in the default...
6
by: SQACSharp | last post by:
I'm using the EnumChildWindows API with an EnumChildWndProc callback to populate the treeview. The output will be something similar to spy+ + How can I specify the parent when adding a new node...
2
by: Ken Fine | last post by:
I want to add the security question and answer security feature to the ChangePassword control. I am aware that this functionality is built into the PasswordRecovery tool. I have implemented the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.