0

My understanding for a long time was that basically never to use ==, since it doesn't respect type and can end up causing undesired results.

I just read this post about the general comparison How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?

but wasn't sure if everyone has the same opinion as me.

Here is the scenario which I encountered:

Original code

$state = (intval($state) === TIMESHEET_STATE_UNLOCKED) ? 'unlock' : 'lock';

We had found that this line was invalid, since our constant TIMESHEET_STATE_UNLOCKED was a boolean and $state was the string '0' or '1'. We could fix this in one of two ways:

$state = (intval($state) === intval(TIMESHEET_STATE_UNLOCKED)) ? 'unlock' : 'lock';

OR

$state = ($state == intval(TIMESHEET_STATE_UNLOCKED)) ? 'unlock' : 'lock';

My first inclination was to former but I'm not so sure anymore.

user1015214
  • 2,167
  • 8
  • 31
  • 52

0 Answers0