1
votes

So I am porting a VBA application to PHP and ran into this wonderful little nugget of code:

expr1 = expr2 Mod expr3 = 0

I thought it was behaving like a ternary operator but when I broke it down to simple if then statements the outcome was not as expected. So I ask the brilliant stackoverflow community to help me out and put it in easy to understand terms. I know by looking at the other answers I will not be let down. [/end brown_nose>]

2
Bad bad code :) Looking at expression, my mind right away assume that this is a comparision in an if / while, etc. statement. Combined that with lack of grouping and VB assigment = comparison operator and dynamic type conversion, hehe. If you are not familiar w/ operator precedence, it's very easy to read that as (expr1 == expr2) % (expr3 == 0)... which can easily result in Divide By Zero. Very bad :)Jimmy Chandra

2 Answers

8
votes

It's assigning expr1 to a boolean value that indicates whether expr2 can be divided evenly (with no remainder) by expr3. Remember that = means == in VB :D.

Here's what it would look like with the implied parentheses:

expr1 = ((expr2 Mod expr3) = 0)
5
votes

It is the modulus operator:

a MOD b = remainder of a/b

in PHP it is the % sign:

a%b

see php documentation here

So the line

expr1 = expr2 Mod expr3 = 0

means: expr1 is true, if expr2 can be divided by expr3 without any remainders: eg:

20 MOD 5 = 0 ==> TRUE
22 MOD 5 = 2 ==> FALSE