0

I am having some trouble understanding how to use Yoda conditions, I roughly get the way it works (Man is it hard finding a youtube video online). Can someone show me how to use Yoda conditions on this use case:

if ($parentId === null) {
            foreach ($allCustomers as $customer) {
                if ($customer['parentCustomer_id'] === '1') {
                    array_push($parentCustomers, $customer);
                }
            }
            if (isset($parentCustomers[0])) {
                $parentId = $parentCustomers[0]['customer_id'];
            }
        }

I understand the idea is it checks whether variables are properly compared to expressions. I just don't understand how to apply it properly.

It would be really helpful if someone could explain their answer so I can learn to apply it myself.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Bradley Kirkland
  • 621
  • 1
  • 6
  • 26
  • The duplicate gives quite a few examples (and even that question has a duplicate which explains if it makes any difference). – Nigel Ren May 09 '21 at 08:29
  • 1
    An example from your code would be `if ($parentId === null) {` => `if ( null === $parentId ) {` – Nigel Ren May 09 '21 at 08:30

0 Answers0