Page 1 of 1

undefined index

Posted: Sun Jun 10, 2012 9:16 am
by wvoyance
I made a query to other site. An array is returned. However, which information is inside is not sure.
Suppose the array normally contains
A["a"],A["b"],A["C"]
but sometimes some of them might be missing. Therefore I got this kind of error message:
============================
Notice: Undefined index: author in /home/public_html/catalog/admin/insert_one.php on line 149
============================
Is there anyway I can let PHP ignore the missing, perhaps simply treat it as blank, without aborting the program?

Re: undefined index

Posted: Sun Jun 10, 2012 12:27 pm
by Tiancris
You can deactivate that message using error_reporting() at the top of your script:

Code: Select all

// Report all errors except E_NOTICE
error_reporting(E_ALL ^ E_NOTICE);
More info about error_reporting(): http://php.net/manual/en/function.error-reporting.php

Re: undefined index

Posted: Sun Jun 10, 2012 4:23 pm
by requinix
Tiancris wrote:You can deactivate that message using error_reporting() at the top of your script:
Or you could actually fix the problem.

OP: use isset or empty to check that the item exists before you try to use it.

Re: undefined index

Posted: Mon Jun 11, 2012 11:26 am
by pickle
Ya, turning off E_NOTICE is a band-aid at best.