0

I'm working on a truthy/falsy where I need to check 2 values, of which one may be null, the other 0. If integer vs string value same, they'd be the same. The simplest I've been able to break it down is:

if($var1 == $var2) {// They may be the same
  if((isset($var1) && !isset($var2)) || (!isset($var1) && isset($var2))) { // Is one set, and not the other?
     return true; // As different as we'll allow
  }
  return false;   // Nope they're the same
}
return true;      // Definitely different

My question would be mainly, given the above code block, is there a simpler way to check if one value is set, and not the other?

EDIT: The reason I check for equality first, is that the value may pass 1 == 10 or 10 == '10', in which case, I don't need to check further, but if null == 0, this would pass and require further checking. I saw that I can pass multiple values to isset, which would work if I required both to be set or both not set, but if one is, and not the other, that's where I need to flag.

Code Ward
  • 13
  • 2
  • 1
    *If* both values are the same, you test whether one of them isn't set…!? Can you simplify your question to clearly tell us what values you expect and what the result should be? – deceze Dec 03 '18 at 14:41
  • 1
    _Small point which lots of people seem to miss/forget_ You can pass more than one value to `isset()` and it will AND them to get the result. [manual](http://php.net/manual/en/function.isset.php) – RiggsFolly Dec 03 '18 at 14:43
  • 1
    @Riggs Very true, but not helpful in this case. :) – deceze Dec 03 '18 at 14:44
  • @deceze Agree'd, but I keep seeing this and felt the urge to void a little :) – RiggsFolly Dec 03 '18 at 14:45
  • The reason I check for equality first, is that the value may pass 1 == 10 or 10 == '10', in which case, I don't need to check further, but if null == 0, this would pass and require further checking. I saw that I can pass multiple values to isset, which would work if I required both to be set or both not set, but if one is, and not the other, that's where I need to flag. – Code Ward Dec 03 '18 at 14:48
  • That ^ belongs in the question as an edit. – Funk Forty Niner Dec 03 '18 at 14:49
  • 1
    First of all, do you actually need `isset` (if so, why), or just `=== null`? – deceze Dec 03 '18 at 14:49
  • @FunkFortyNiner Updated edit, thank you. – Code Ward Dec 03 '18 at 14:54

2 Answers2

2

Using the null coalescing operator may come in handy...

return ($var1 ?? null) !== ($var2 ?? null);

Checks that they are not equal and at least one is defined and not null.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
0

You are checking for equality before checking if they are set.

I'd recommend checking if they are set first.

If the PURE goal is to check if one is set and not the other, then:

return ( ( empty($var1) && ! empty($var2) ) || ( ! empty($var1) && empty($var2) );

Per comments, isset is the requirement, so this version:

return ( ( isset($var1) && ! isset($var2) ) || ( ! isset($var1) && isset($var2) );

And, props to @deceze to suggest an even simpler version:

return ( isset($var1) != isset($var2) );
random_user_name
  • 25,694
  • 7
  • 76
  • 115