[PHP] Strange Array Error
Hi, I have been trying for the past two hours to understand why I get an error with the following code: '; } ?> Fatal error: Unsupported operand types in /usr/home/expensesystem/public_html/test.php on line 5 I am trying to create a loop where $i is added to array element $i, and can't find any information relating to this on google or php.net... Thanks for your advice. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange Array Error
On Thu, 21 Oct 2004 14:28:26 +0100, Shaun <[EMAIL PROTECTED]> wrote: > I have been trying for the past two hours to understand why I get an error > with the following code: > > $test_array[] = array(); $test_array = array(); > $i = 0; > while($i < 7){ > $test_array[$i] += $i; $test_array[$i] = $i++; // maybe what you want ? > echo '$day_total[$i] = '.$day_total[$i].''; > } > ?> > > Fatal error: Unsupported operand types in > /usr/home/expensesystem/public_html/test.php on line 5 > > I am trying to create a loop where $i is added to array element $i, and > can't find any information relating to this on google or php.net... -- Greg Donald Zend Certified Engineer http://gdconsultants.com/ http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange Array Error
Shaun The problem is that you've assigned $test_array[] = array(); This means that the first element ($test_array[0]) is an array. You're then trying to add $i to it, as in $test_array[$i] = $test_array[$i] + 1; in terms of types what you're doing is array = array + integer Which doesn't make any sense. If you change the first line to $test_array = array(); Then the individual elements will all be NULL, which evaluates to 0 in an integer context, and you will get the result you expect. Hope this helps Cheers Chris Shaun wrote: Hi, I have been trying for the past two hours to understand why I get an error with the following code: '; } ?> Fatal error: Unsupported operand types in /usr/home/expensesystem/public_html/test.php on line 5 I am trying to create a loop where $i is added to array element $i, and can't find any information relating to this on google or php.net... Thanks for your advice. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Strange Array Error
[snip] > > Hi, > > I have been trying for the past two hours to understand why I get > an error > with the following code: > > $test_array[] = array(); > $i = 0; > while($i < 7){ > $test_array[$i] += $i; > echo '$day_total[$i] = '.$day_total[$i].''; > } > ?> > > Fatal error: Unsupported operand types in > /usr/home/expensesystem/public_html/test.php on line 5 > > I am trying to create a loop where $i is added to array element $i, and > can't find any information relating to this on google or php.net... > > Thanks for your advice. Try this: '; $i++; } ?> PHP objected to the use of += You did NOT want to increment $i before doing the echo anyway, did you? Graham -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange Array Error
You are making the first element of the array $test_array and array item. An array inside and array. $test_array[] = array() is the same as $test_array[0] = array() So what you are trying to do is add $i to an array. Kind of like trying to figure out what happens when you add 1 to the color red. Try one of these: $test_array = array() or $test_array = array(0,0,0,0,0,0,0) Basically, drop the brackets. You use brackets to reference and array element, not the array itself. On Oct 21, 2004, at 9:28 AM, Shaun wrote: Hi, I have been trying for the past two hours to understand why I get an error with the following code: '; } ?> Fatal error: Unsupported operand types in /usr/home/expensesystem/public_html/test.php on line 5 I am trying to create a loop where $i is added to array element $i, and can't find any information relating to this on google or php.net... Thanks for your advice. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Brent Baisley Systems Architect Landover Associates, Inc. Search & Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange Array Error
That is, expected results, *apart* from the infinite loop you'll have due to not incrementing $i Cheers Chris Chris Dowell wrote: Shaun The problem is that you've assigned $test_array[] = array(); This means that the first element ($test_array[0]) is an array. You're then trying to add $i to it, as in $test_array[$i] = $test_array[$i] + 1; in terms of types what you're doing is array = array + integer Which doesn't make any sense. If you change the first line to $test_array = array(); Then the individual elements will all be NULL, which evaluates to 0 in an integer context, and you will get the result you expect. Hope this helps Cheers Chris Shaun wrote: Hi, I have been trying for the past two hours to understand why I get an error with the following code: '; } ?> Fatal error: Unsupported operand types in /usr/home/expensesystem/public_html/test.php on line 5 I am trying to create a loop where $i is added to array element $i, and can't find any information relating to this on google or php.net... Thanks for your advice. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Strange Array Error
Oh @#*! Thanks Greg, I always get that one wrong ! $test_array = array(); :) > -Original Message- > From: Graham Cossey [mailto:[EMAIL PROTECTED] > Sent: 21 October 2004 14:48 > To: Shaun; [EMAIL PROTECTED] > Subject: RE: [PHP] Strange Array Error > > > [snip] > > > > Hi, > > > > I have been trying for the past two hours to understand why I get > > an error > > with the following code: > > > > > $test_array[] = array(); > > $i = 0; > > while($i < 7){ > > $test_array[$i] += $i; > > echo '$day_total[$i] = '.$day_total[$i].''; > > } > > ?> > > > > Fatal error: Unsupported operand types in > > /usr/home/expensesystem/public_html/test.php on line 5 > > > > I am trying to create a loop where $i is added to array element $i, and > > can't find any information relating to this on google or php.net... > > > > Thanks for your advice. > > Try this: > $test_array[] = array(); > $i = 0; > while($i < 7){ > $test_array[$i] = $i; > echo '$test_array[$i] = '.$test_array[$i].''; > $i++; > } > ?> > > PHP objected to the use of += > You did NOT want to increment $i before doing the echo anyway, did you? > > Graham > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Strange Array Error
On Thu, 2004-10-21 at 15:04 +0100, Graham Cossey wrote: > Oh @#*! > Thanks Greg, I always get that one wrong ! > > $test_array = array(); > > :) a few less lines version: $test_array = array(); for ($i=0; $i<7; $i++) $test_array[$i] = $i; print_r($test_array); hth, -Robby -- /*** * Robby Russell | Owner.Developer.Geek * PLANET ARGON | www.planetargon.com * Portland, OR | [EMAIL PROTECTED] * 503.351.4730 | blog.planetargon.com * PHP/PostgreSQL Hosting & Development / signature.asc Description: This is a digitally signed message part
Re: [PHP] Strange Array Error
On Thu, 21 Oct 2004 14:28:26 +0100, Shaun <[EMAIL PROTECTED]> wrote: > Hi, > > I have been trying for the past two hours to understand why I get an error > with the following code: > > $test_array[] = array(); //Change it to $test_array=array(); /*Your statement adds an array to the current+1 position of the array since you've put the [] after the variable the PHP interpreter initializes it as an array type of variable so it takes that action*/ > $i = 0; > while($i < 7){ > $test_array[$i] += $i; > echo '$day_total[$i] = '.$day_total[$i].''; // Change the single quotes to double quotes // i.e. echo "{$day_total[$i]}={$day_total[$i]}"; // Add ++$i; here > } > ?> Notes: -Always use double quotes if you want to concatanate vars with strings. -Whe ur using arrays in ur strings that is to be concatenated delimit the array by { and }. -Take a look at sprintf function to better format strings in case ur familiar with C coding style. -Sometimes googling might not help nor PHP.net. So, the PHP manual is ur friend. e.g you could go to Array section in PHP manual and find out what's up :) > > Fatal error: Unsupported operand types in > /usr/home/expensesystem/public_html/test.php on line 5 > > I am trying to create a loop where $i is added to array element $i, and > can't find any information relating to this on google or php.net... > > Thanks for your advice. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Hope that helps. -- M.Saleh.E.G 97150-4779817 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php