Member Avatar for LastMitch

Hi

I'm trying figure why I'm getting this error Undefined offset: 1.

I'm practicing with list() function and with a explode() function.

When I ran the code it kept mention this error:

Undefined offset: 1 

The line is this:

list($bagsize, $candyprice)=explode("#", $_GET["size"]);

The list is not an array. It's on the database:

$bagsize = $_GET["size"]);
$candyprice = $_GET["price"]);

But the thing is that I don't understand why is it offset.

I google it and read a few threads and find out it varies.

I just want to prevent this error from happening again.

Any Suggestions and explanations will help. I appreciate it. Thanks!

Notice: Undefined offset: 1

You're not getting an Error, but a Notice.This is caused because your $_GET["size"] doesn't contain "#" anywhere in the string so it contains only one element in array.list() is used to assign a list of variables in one operation.Your list contains 2 elements but as from data returned by implode,there is only one data.So it throws a notice.
A way to overcome that is to use array_pad() method.

list($bagsize, $candyprice)= array_pad(explode("#", $_GET["size"]),2,null);
commented: Thanks for the explanation & solution! +12
Member Avatar for LastMitch

@IIM

Thanks for the reply and explanation. I'll be honest with you I don't get much of these Notice mostly I get is an error so I know how to debug it. But for this thing (notice) I have a hard time understanding. I will make some adjustment.

Member Avatar for LastMitch

@IIM

It worked! The Notice disappear. I just want to say Thanks for the explanation and solution.

The array_pad() function was something new! I don't used that often.

I learn something new I appreciated your help! Thanks

@lastmitch: echo the content of $_GET["size"] for which you are getting error.Also post it here.

Member Avatar for LastMitch

@lastmitch: echo the content of $_GET["size"] for which you are getting error

There's no Notice nor error. Everything is good! Thanks. IIM.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.