bwaaaaaa bwaaaaaa - 5 months ago 10
PHP Question

PHP basic operator

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)."\n"; // prints 2
echo (5 % -3)."\n"; // prints 2
echo (-5 % 3)."\n"; // prints -2
echo (-5 % -3)."\n"; // prints -2

Answer

You're trying to get a modulo of negative number. There's no difference between this and: 20%8

See: http://math.stackexchange.com/questions/519845/modulo-of-a-negative-number

Comments