How to avoid undefined index errors on a function to detect undefined indexes in PHP?
one text
Okay, so I wrote a function to test if an array key has a value and return it, or else, return a value from the POST array, the problem is, since some of the are undefined, I get "Undefined Index error" while I'm sending the variables to see if they're defined or not.
So, what I want to know is, how to prevent this error from happening without checking if it is set before sending it to the function?
Here's some of the code I'm using:
function issetorpost($var, $post = '') {
$return = '';
if (isset($var) && !empty($var)) {
$return = $var;
}
if (isset($post) && !empty($post)) {
$return = $post;
}
return $return;
}
And it's used on input checks on form:
<input type="text" name="inp1" value="<?= issetorpost($fromdb['inp1'],$_POST['inp1']) ?>" />
Source