Page 1 of 1

syntax help logic operator

Posted: Wed Mar 17, 2010 5:53 pm
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

Re: syntax help logic operator

Posted: Wed Mar 17, 2010 7:26 pm
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.

Re: syntax help logic operator

Posted: Wed Mar 17, 2010 7:26 pm
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.