What is the PHP operator % and how do I use it in real

What is the explanation for PHP's operator % in full detail?

Including examples would be nice!


It's the modulus operator, which gives the integer remainder of a division eg

7 / 2 = 3.5  // 3 remainder 1
7 % 2 = 1    // the remainder

Obvious real world example is working out whether a number is odd or even

if (($n % 2) == 0) the number is even, else it's odd... useful when you want to show alternate rows in a table in different colours


% is the modulus operator.

An example

$num1 = 160;
$num2 = 15;
$result = $num1 % $num2;
echo "The modulus of these numbers is $result";

It's the modulus operator. It gives you the "remainder" after a division. It's a fairly standard operator.

Here is the PHP reference for Arithmetic Operators.

链接地址: http://www.djcxy.com/p/1724.html

上一篇: 在PHP中双重(!!)运算符

下一篇: 什么是PHP运营商?我如何真正使用它