PHP Notice: Uninitialized string offset

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

PHP Notice: Uninitialized string offset

Post 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
Last edited by bouncer on Fri Jul 20, 2007 10:15 am, edited 1 time in total.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

use this instead

if (isset($array['key'])) ;
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post 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
Last edited by bouncer on Thu Jul 19, 2007 10:26 am, edited 1 time in total.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

do a print_r() on your array and paste the output here.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Post by Chalks »

woah, I've never seen <> before. What language(s) is that from?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

In MySQL it is equivalent to !=
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Post by Chalks »

:oops:

I should have figured that one out. :D
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

thank you guys, i've already fix that issue :oops: .

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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The zero should be a hint.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

feyd wrote:The zero should be a hint.
sorry for my ignorance but, what do you mean with that ?

thanks in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's a zero in "$dayResult[0]['productID']" and a zero referenced in the error.
Post Reply