thanksd
thanksd

Reputation: 55644

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

Upvotes: 3

Views: 2796

Answers (1)

Jeroen Noten
Jeroen Noten

Reputation: 3624

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.

Upvotes: 2

Related Questions