Same array in and out of custom function

Hello,

If:

function custom($var) {
  echo $errors[$var];
}

And:

$errors = array();
$errors['fn'] = 'You forgot to enter your name.';

Then:

custom('fn');

Why doesn’t it work? Am I needing to address scope? If so, how would I do this?

Thanks in advance for any help you can provide :smiley:

Yep, it’s a scope problem.

Two possible solutions:


function custom($var) { 
  $errors = array();
  $errors['fn'] = 'You forgot to enter your name.';
  echo $errors[$var];
}

custom('fn');


function custom($var, $errors) { 
  echo $errors[$var];
}

$errors = array();
$errors['fn'] = 'You forgot to enter your name.';
custom('fn', $errors);

Beauty! Used your second example and fixed everything!

Cheers :smiley: