# How is -13 % 64 = -13 in PHP?

Derived from this question : (Java) How does java do modulus calculations with negative numbers?

Anywhere to force PHP to return positive 51?

update
Looking for a configuration setting to fix, instead hard-guessing

Or other math function like bcmath?

updated
Not entire convinced by that java answer, as it does not take account of negative modulus `-13+(-64) =?`

83

If GMP is available, you can use `gmp_mod`

Calculates n modulo d. The result is always non-negative, the sign of d is ignored.

Example:

``````echo gmp_strval(gmp_mod('-13', '64')); // 51
``````

Note that `n` and `d` have to be GMP number resources or numeric strings. Anything else won't work¹

``````echo gmp_strval(gmp_mod(-13, 64));
echo gmp_mod(-13, 64);
``````

will both return -51 instead (which is a bug).

¹ running the above in this codepad, will produce 51 in all three cases. It won't do that on my development machine.

Wednesday, March 31, 2021

44

Well, I figured this out quite a long time ago: here's the answer. A guy made a workaround. First, he separate the macros files (VBA) from the sheet, then he left the PHPExcel "doing his thing", e then put it back (the macros!). It was enough for me! :D

Wednesday, March 31, 2021

46

Modulus returns the remainder, not a boolean value.

This code will resolve to `true` for `1, 3, 6, 9, ...`

``````if ((\$k % 3 == 0) || \$k==1 ) { echo '<div class="modcontainer">'; }
``````

This code will resolve to `true` for `1, 4, 7, 10, ...`

``````if (\$k % 3 == 1) { echo '<div class="modcontainer">'; }
``````
Wednesday, March 31, 2021

48
``````\$excel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
``````

Should set the row height to 'auto' for row 1.

Wednesday, March 31, 2021

83

It's giving you the reminder of the division what you need is fmod,

fmodReturns the floating point remainder (modulo) of the division of the arguments

``````echo fmod(877.5, 1); // 0.5
``````
Saturday, May 29, 2021