Recently I saw programmer put literal in front of if condition
if ( -99 == $poll_id )
Why they did that? What is the difference with normal if e.g.:
if ( $poll_id == -99 )
Recently I saw programmer put literal in front of if condition
if ( -99 == $poll_id )
Why they did that? What is the difference with normal if e.g.:
if ( $poll_id == -99 )
This an idiom that is borrowed from C. It is called Yoda Conditions.
The idea behind the idiom is avoiding errors when you type one equal sign instead of two.
When you write
if ( x = 3 )
instead of
if ( x == 3 )
The code would compile, but work incorrectly.
If you get in the habit of turning the condition around, the code with a single equal sign would not compile.