undefined index

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

Post Reply
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

undefined index

Post 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?
User avatar
Tiancris
Forum Commoner
Posts: 39
Joined: Sun Jan 08, 2012 9:54 pm
Location: Mar del Plata, Argentina

Re: undefined index

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: undefined index

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: undefined index

Post by pickle »

Ya, turning off E_NOTICE is a band-aid at best.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply