Why on Earth am I getting “undefined_index” errors?

It’s a common PHP error, usually when you try to access an array member with a non-existent key;

$array = array( 'hello' => 'world' );
echo $array['foobar']; // undefined index

You should check for the key first with isset( $array['foobar'] );

UPDATE: In this case, I would chuck in a loop that sets-up the variables for you, checking for the index in the process.

foreach ( array( 'genus', 'species', 'etymology', 'family', 'common_names' ) as $var )
    $$var = isset( $custom[ $var ][0] ) ? $custom[ $var ][0] : '';

echo $genus; // prints value of $custom['genus'][0] if set, otherwise empty

Leave a Comment