Search code examples
phpvariablescoding-styleternary

true or false as result of php ternary operator


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.)?


Solution

  • 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.