PHP基本操作符
Just came across this problem, would you mind explaining how this end up having an answer of 4? So basic yet I have no idea how it happened.
echo $a = 20%-8; //4
Answer:
echo (5 % 3)."
"; // prints 2
echo (5 % -3)."
"; // prints 2
echo (-5 % 3)."
"; // prints -2
echo (-5 % -3)."
"; // prints -2
刚刚遇到这个问题,您是否介意解释这最终如何得到4的答案? 基本但是 我不知道它是怎么发生的。 p>
echo $ a = 20%-8; // 4
code> pre>
答案: p>
echo(5%3)。“
”; //打印2
echo(5%-3)。“
”; //打印2
echo(-5%3)。“
”; //打印-2
echo(-5%-3)。“
”; //打印-2
code> pre>
div>
You're trying to get a modulo of negative number. There's no difference between this and: 20%8
See: https://math.stackexchange.com/questions/519845/modulo-of-a-negative-number
% operator will leave the reminder of the operation. That is $x % $y
will result in the remainder of $x
divided by $y
.
Here, when 20 divided by -8, it will leave reminder 4 which is the answer.
@bwaaaaaa,Modulus operator will give the reminder of the operation.
Suppose $x % $y, that means that Remainder of $x divided by $y.
So for your example when 20 divided by -8 will gives you reminder 4 so its output is 4.
For More info. regarding modulas operator please visit this
The remainder operator returns the remainder left over when one operand is divided by a second operand. remember that the modulo operator result would always takes the sign of the dividend, not the divisor. It uses a built-in modulo function to produce the result, which is the integer remainder of dividing var1 by var2.
So here in your code the var1 is 20 which is positive integer and var2 is -8 which is negative integer. 20 is dividend and -8 is divisor.
so As we know the module operator result would take the sign of the dividend, not the divisor. That's why reminder answer will be just 4. Hope so now it is clear. thank you