haggholm: (Default)
Petter Häggholm ([personal profile] haggholm) wrote2008-02-05 11:11 am
Entry tags:

(no subject)

This is why PHP fails to make me happy. —Well, that's not quite accurate: Say rather that this is one of the many ways in which it so fails:

// This gives me one result...
$should_match = ( $desired_value and ($db_value == PREF_TYPE_YES)) or
                (!$desired_value and ($db_value == PREF_TYPE_NO));

// ...This gives me another result. Note the identical logic.
if (( $desired_value and ($db_value == PREF_TYPE_YES)) or
    (!$desired_value and ($db_value == PREF_TYPE_NO)))
{
	$should_match = true;
}
else
{
	$should_match = false;
}

What am I missing?

Update:

David poked at this after I threw my hands up in disgust. It turns out to be a good old precedence issue, since, sensibly enough, or does not have the same precedence as ||, and similarly, and is different from &&. Therefore,

$x = true or false;
// is not equivalent to
$x = true || false;
// but instead to
($x = true) or false;
// rather than the
$x = (true or false);
// that I expected, and that || provides.

I see my mistake, but whoever made this design decision: I hate you.


Post a comment in response:

If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org