Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP & ZF2 / Comparison: expected == actual

does anyone know why some developers (especially seen in the sources of Zend Framework 2) write the expected value before the actual value in comparisons?

Example:

if (true === $actualValue) { ... }

instead of

if ($actualValue === true) { ... }

This case is not defined in the PSR coding standard.

Note: There is a similar topic for c++ but without really helpful answers.

like image 869
Daniel M Avatar asked Dec 26 '22 19:12

Daniel M


1 Answers

What you are seeing is Yoda conditions. There is no standard defining these (at least not to my knowledge). They are merely a way to protect yourself against a common coding error (assignment in your conditions).

Example:

if( number = 4 ) // Works perfectly

if( 4 = number ) // Throws an exception
like image 105
Kristoffer Sall-Storgaard Avatar answered Jan 08 '23 15:01

Kristoffer Sall-Storgaard