3

I'm working with legacy PHP code and I'm seeing a lot of instances where the programmer has done this:

$foo = ($bar === 'baz') ? true : false;

instead of:

$foo = ($bar === 'baz');

Is there ever a case of a conditional expression where the first example wouldn't function the same as the second? Is there any common reason for doing the first (readability, defensive coding, etc.)?

thanksd
  • 54,176
  • 22
  • 157
  • 150

1 Answers1

2

They are functionally equivalent. Readability may be a reason to write it the first way, it may be easier to see that the result will be a boolean, but that is subjective.

Jeroen Noten
  • 3,574
  • 1
  • 17
  • 25