[thelist] Redirection

Jackson Yee jyee at vt.edu
Fri Jul 26 18:20:00 CDT 2002


----- Original Message -----
From: "John Corry" <webshot at members.evolt.org>
To: <thelist at lists.evolt.org>
Sent: Friday, July 26, 2002 18:31
Subject: RE: [thelist] Redirection


> I've always had trouble with:
> if ($x == 'this' || 'that'), even though according to the docs it seems
> like it should work.

The reason why it doesn't work is because of operator precedence - you'll find
the same result in Java or C++, which PHP borrows heavily for syntax.

http://www.php.net/manual/en/language.operators.php#language.operators.precedence

In the expression

$x == 'this' || 'that',

the || operator has higher precedence than the == operator, thus when PHP goes
over the statement, it will evaluate the expression

'this' || 'that'

before the expression

$x == 'this'

'this' || 'that' will evaluate to a boolean true since PHP considers nonempty
strings to be true values, and thus you'll be left with the expression

$x == true

which will result in true as long as $x is nonempty, meaning that the whole if
statement will always be true when a valid domain name is specified in $x.

I haven't used any programming languages before where you could write

if ($x == 'this' || 'that')

and have it interpreted as

if ($x == 'this' || $x == 'that')

but I would be very interested someone else has.

Regards,
Jackson Yee
jyee at vt.edu
http://www.jacksonyee.com/




More information about the thelist mailing list