1

I have this code in PHP

$customer->setGroupId($params['group_id']);

I get undefined index group_id error when I run it. I want that if it is undefined then do not throw error but continue the code to work.

How is it possible?

JKhan
  • 1,157
  • 4
  • 14
  • 23
  • Use `$params['group_id'] ?? null` – ArSeN Aug 17 '21 at 20:06
  • 1
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – ArSeN Aug 17 '21 at 20:06

1 Answers1

2

You can check it with isset() function, for example:

if(isset($params['group_id']) {
   $customer->setGroupId($params['group_id']);
}else {
   // Something
}

If you want to check for the empty field, you should use empty() function too:

if(!empty($params['group_id']) {
   $customer->setGroupId($params['group_id']);
}else {
   // Something
}
Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58