Page 1 of 2
PHP Notice: Uninitialized string offset
Posted: Thu Jul 19, 2007 10:11 am
by bouncer
hi.
i'am having some problems resolving this issue, everytime i load an array and them i test if a value is set in a variable, i get the following notice, "PHP Notice: Uninitialized string offset: 0"
Code: Select all
//$dayResult is an array
$dayResult = $dbprod->returnProductInfo($today, $productCod, $_site);
//$dayResult[0]['productID'] is a string
if ( $dayResult[0]['productID'] != "" ) { --> PHP Notice: Uninitialized string offset: 0
...
}
can someone tell how can i remove this notice ?
regards
Posted: Thu Jul 19, 2007 10:17 am
by Begby
use this instead
if (isset($array['key'])) ;
Posted: Thu Jul 19, 2007 10:24 am
by bouncer
Begby wrote:use this instead
if (isset($array['key'])) ;
i've already try that and no progress
Code: Select all
//$dayResult is an array
$dayResult = $dbprod->returnProductInfo($today, $productCod, $_site);
if ( isset ( $dayResult['productID'] ) ) { --> cant get the $dayResult[0]['productID'] value
...
}
regards
Posted: Thu Jul 19, 2007 10:25 am
by Begby
do a print_r() on your array and paste the output here.
Posted: Thu Jul 19, 2007 10:29 am
by bouncer
i get this
Code: Select all
Array ( [0] => Array ( [id] => 14 [date] => 2007-07-30 [productID] => 13349 [cod] => 408081 [price] => 389 [description] => [auxiliar] => 0 [site] => 1 )
regards
Posted: Thu Jul 19, 2007 10:42 am
by Begby
Are you doing print_r($dayResult) right there in your code above the if?
The notice you are getting is when you try to access a string by a character and its past the end of the string, you will get the error if you do this
Code: Select all
$string = 'test' ;
echo $string[2] ; // echos 's'
echo $string[7] ; // echos '' but gives you a notice
Why that is doing that in your code I haven't a clue. Also try doing var_export or var_dump on your dayResult right before the if statement.
Also, are you sure the notice is happening on that exact line? When I make a string and try to access it like $string[0]['key'] it throws a different error instead of that notice.
Posted: Thu Jul 19, 2007 10:51 am
by bouncer
if i do,
Code: Select all
var_dump($dayResult[0]['productID']); --> string '13349'
var_dump($dayResult['productID']); --> NULL
but in the if statment it throws this notice, dont know why

, and everything is working right
regards
Posted: Thu Jul 19, 2007 12:12 pm
by Begby
HAHAH... whoops
not equals is != in php, not <>. I am sorry I put you through the trouble and didn't notice that from the start. Give that a try
Posted: Thu Jul 19, 2007 12:21 pm
by Chalks
woah, I've never seen <> before. What language(s) is that from?
Posted: Thu Jul 19, 2007 12:28 pm
by Benjamin
In MySQL it is equivalent to !=
Posted: Thu Jul 19, 2007 12:29 pm
by Chalks
I should have figured that one out.

Posted: Fri Jul 20, 2007 10:17 am
by bouncer
thank you guys, i've already fix that issue

.
but i continue with the same problem: "PHP Notice: Uninitialized string offset: 0". anyone knows what i'm doing wrong in,
Code: Select all
//$dayResult is an array
$dayResult = $dbprod->returnProductInfo($today, $productCod, $_site);
//$dayResult[0]['productID'] is a string
if ( $dayResult[0]['productID'] != "" ) { --> PHP Notice: Uninitialized string offset: 0
...
}
thanks in advance
Posted: Fri Jul 20, 2007 5:44 pm
by feyd
The zero should be a hint.
Posted: Mon Jul 23, 2007 8:39 am
by bouncer
feyd wrote:The zero should be a hint.
sorry for my ignorance but, what do you mean with that ?
thanks in advance
Posted: Mon Jul 23, 2007 2:32 pm
by feyd
There's a zero in "$dayResult[0]['productID']" and a zero referenced in the error.