Sunday, 15 May 2011

PHP order of logical operators -



PHP order of logical operators -

i'm curious how php handles conditional statements / order of operations nesting. if utilize next if condition:

if(x == (1 || 2)) { // }

i expect behave same as

if(x == 1 || x == 2) { // }

...but doesn't. first illustration seems handy shorthand makes pretty sense, doesn't expect. can shed lite on issue? php first statement?

so piece of code:

if ($x == ( 1 || 2)) { // }

in php, non-zero number considered true. disclaimer: fact isn't true in other languages. in php, 0 number considered false. you're asking if $x == true in above piece of code.

hence, whenever $x number other 0 statement within if resolve true. however, when $x = 0 equivalent saying false == true of course of study resolve false.

this article might help: php: booleans

php

No comments:

Post a Comment