-1

I want to check that the key - value pair 'purpose' => 'toggle' is not in an array of options for an element with the type checkbox.

It is possible that there is only the key 'purpose', only the value 'toggle', both as a pair, both not as a pair or neither of them.

My code looks like this:

if ($type === 'checkbox'
   && ! (array_key_exists('purpose', $options) && $options['purpose'] === 'toggle'))
{ ... }

It seems to work, but i was wondering if there is a more efficient way to do that (since those are a lot of calculations that have to be done for every element).

  • if you don't want 'purpose' key in your array $options, then why to search for $options['purpose'] for 'toggle'? –  Nov 09 '17 at 08:51
  • where that brackets close? –  Nov 09 '17 at 08:53
  • try $count=0; foreach($dataArry as $index1=>$data){ if(($index1=='purpose')&&($data=='toggle')){ $count++; // Got that } } echo $count; – Confused Nov 09 '17 at 08:54
  • It's supposed to meet the condition if any other value but 'toggle' is set for 'purpose'. So something like ```$options['purpose'] = 'random purpose' ``` should return true... – Jennifer Mourek Nov 09 '17 at 08:55
  • For php7 - would `$options['purpose']??null === 'toggle'` (using null coalesce operator) do the job? – Nigel Ren Nov 09 '17 at 08:55
  • you should use || instead of && in between searching purpose key and checking toggle –  Nov 09 '17 at 08:57
  • @AnimeshSahu there is a not (`!`) operator before the brackets – Kaddath Nov 09 '17 at 09:00
  • ohkkkk!! trying to solve –  Nov 09 '17 at 09:01
  • slight optimization, but `isset($options['purpose'])` seems faster than `array_key_exists` ([reference here](https://blog.yoda-bzh.net/index.php?post/2010/02/04/PHP-bench%3A-isset-vs-array_key_exists)). In your case, you don't need to bother about their difference with null values, because you search for a specific value if set – Kaddath Nov 09 '17 at 09:02
  • what if trying to search for not having purpose key in options var, then why to search for toggle..... Try to make standard php –  Nov 09 '17 at 09:03
  • _“(since those are a lot of calculations that have to be done for every element)”_ - meaning what exactly? Are you performing the same check for multiple elements of the checkbox type? If so, can the options array be changed in between? If not, then of course the logical thing to do would be to perform that check once, and store the result in a variable. – CBroe Nov 09 '17 at 09:04
  • @Kaddah I actually didn't think about ```isset()```, but that's a good one, thanks! – Jennifer Mourek Nov 09 '17 at 09:19
  • @CBroe I run this over every element in all of my forms to see what decorators to apply to them. So regular checkboxes have the "checkbox" decorator and the "toggle" boxes i use for the checkbox toggle hack. – Jennifer Mourek Nov 09 '17 at 09:19

1 Answers1

0
if ( $type === 'checkbox' && @$options['purpose'] != 'toggle' )  
{ ... }
Lexxusss
  • 542
  • 4
  • 10
  • Using `@` in any PHP code is (IMHO) bad coding. It may ignore other sorts of errors which are not within the intended scope of the problem. – Nigel Ren Nov 09 '17 at 09:27
  • That looks a lot less complicated and seems to work. I'll run some tests and do some research but so far I'm happy with your solution :) – Jennifer Mourek Nov 09 '17 at 09:30
  • Using @ is really bad practice in most cases. Actually it will do the same as your code. And in this custom case it can be done without any consequences. – Lexxusss Nov 09 '17 at 09:39
  • It does seem to serve the purpose and not slow my form generation down too much. Hacky solution but does the job. :) – Jennifer Mourek Nov 09 '17 at 09:40