1 3 Votes

PHP: Meaning of the Percent Sign as Operator

Question by Guest | 05/06/2014 at 15:19

I just sniffed a bit in a foreign PHP code and discovered an operator which was not known to me so far.

It is the percent sign:

$v = $x % 10;

Obviously, it is used just like the operators +, -, * and /. But what can it mean?

ReplyPositiveNegative

SimplyMe

Show Profile | Message
Avatar
2Best Answer
2 Votes

The percent sign is the modulus operator (MOD), which is used not only in PHP but also in many other programming languages.

The modulus operator always returns the remainder of a division operation.

Here is a small example:

12 % 10 = 2 (10 fits one times in 12, that is a remainder of 2)

10 % 5 = 0 (5 fits two times in 10, without remainder)

3 % 7 = 3 (7 does not fit any times in 3, so 3 remains)

This operator can be used, for example, to create recurring rows of numbers. For instance, when increasing the variable $i within a loop, at each iteration, $i%2 alternating is 0 or 1.
07/06/2014 at 22:15

ReplyPositive Negative
Reply

Related Topics

Meaning of AFAIK?
Question | 1 Answer
"G" or "E" sign on my mobile
Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.