tags:

views:

61

answers:

5
<h1><?php echo $GLOBALS['translate']['About'] ?></h1>
Notice: Undefined index: About in page.html on line 19

Is it possible to "catch" an undefined index so that I can create it (database lookup) & return it from my function and then perform echo ?

A: 

Yes. Use isset to determine if the index is defined and then, if it isn't, you can assign it a value.

if(!isset($GLOBALS['translate']['About'])) {
    $GLOBALS['translate']['About'] = 'foo';
    }
echo "<h1>" .  $GLOBALS['translate']['About'] . "</h1>";
Rupert
this is a very sloppy post: a spurious `)`, missing `;`, missing open/close tags...
mvds
Whooops... that was indeed pretty bad. Thanks for the catch.
Rupert
A: 

try something like:

if ( !isset($GLOBALS['translate']['About']) )
{
    $GLOBALS['translate']['About'] = get_the_data('About');
}
echo $GLOBALS['translate']['About'];
mvds
+1  A: 

The easiest way to check if a value has been assigned is to use the isset method:

if(!isset($GLOBALS['translate']['About'])) {
    $GLOBALS['translate']['About'] = "Assigned";
}
echo $GLOBALS['translate']['About'];
jkilbride
A: 

that wouldn't be the right thing to do. i would put effort into building the array properly. otherwise you can end up with 1000 db requests per page.

also, you should check the array before output, and maybe put a default value there:

<h1><?php echo isset($GLOBALS['translate']['About'])?$GLOBALS['translate']['About']:'default'; ?></h1>
kgb
A: 

You can check if this particular index exists before you access it. See the manual on isset(). It's a bit clumsy as you have to write the variable name twice.

if( isset($GLOBALS['translate']['About']) )
    echo $GLOBALS['translate']['About'];

You might also consider changing the error_reporting value for your production environment.

svens