syntax help logic operator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dennisfreud
Forum Newbie
Posts: 1
Joined: Wed Mar 17, 2010 5:50 pm

syntax help logic operator

Post by dennisfreud »

HI all,
I have this in a snippet:
($_FILES[$fieldname]['error'] == 0)
or error($errors[$_FILES[$fieldname]['error']], $uploadForm);

and I do NOT understand one bit what it does.
$_FILES is clear,
function error is clear,

but how does

(a==b)
or (do something);

work. Some alternative if statement syntax perhaps?
Clueless
Dennis
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: syntax help logic operator

Post by requinix »

Code: Select all

($_FILES[$fieldname]['error'] == 0) or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
is just shorthand for

Code: Select all

if (!($_FILES[$fieldname]['error'] == 0)) error($errors[$_FILES[$fieldname]['error']], $uploadForm);
With A or B, if A is true then it doesn't matter what B is because the result will still be true. PHP won't even bother looking at B is that's the case: it's called short-circuit evaluation. However if A is false then it has to look at B to get a result for the expression: it'll evaluate the error(...) code.

The effect is that if there is no error, nothing happens, but if there is then error() will be called and the script will (probably) terminate.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: syntax help logic operator

Post by Christopher »

Yes, it is just normal operator precedence. If the left side of an OR is false the right side is never executed. If the left side is true then the right side is executed. It is not really explained in the manual, but you can look at the for exit() and the @ operator.
(#10850)
Post Reply