Page 1 of 1
Undefined Index
Posted: Mon Feb 28, 2005 4:35 pm
by Pyrite
Why does:
Return an Undefined Index if bar doesn't not exist (yet). How can I write my scripts to not have to deal with this issue?
Posted: Mon Feb 28, 2005 4:40 pm
by feyd
Code: Select all
$foo = (isset($_POSTї'bar']) ? $_POSTї'bar'] : 'default value');
smpdawg posted a function for this in the last day..
Posted: Mon Feb 28, 2005 4:44 pm
by Pyrite
But why does it happen? Why even issue a notice saying that an array is yet to be created, yet it still works anyways. That is just dumb.
In my case, I often use it in a switch. And hae a default: in the switch in case it isn't defined yet. Unless I turn Notices off in error_reporting, this is bad for me. sucks.
Posted: Mon Feb 28, 2005 4:57 pm
by feyd
They get a default value of NULL when the index is undefined. It's typically not a big issue using an undefined value, but it is good to know that you are using one in case you have a bug related to it..