php PHP中的百分号是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1934173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:20:34  来源:igfitidea点击:

What does the percent sign mean in PHP?

phpoperatorsmodulo

提问by Andrew

What exactly does this mean?

这到底是什么意思?

$number = ( 3 - 2 + 7 ) % 7;

回答by zombat

It's the modulusoperator, as mentioned, which returns the remainder of a division operation.

如前所述,它是运算符,它返回除法运算的余数。

Examples: 3%5returns 3, as 3 divided by 5 is 0 with a remainder of 3.

示例: 3%5返回 3,因为 3 除以 5 为 0,余数为 3。

5 % 10returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5.

5 % 10返回 5,出于同样的原因,10 进入 5 次零,余数为 5。

10 % 5returns 0, as 10 divided by 5 goes exactly 2 times with no remainder.

10 % 5返回 0,因为 10 除以 5 正好 2 次,没有余数。

In the example you posted, (3 - 2 + 7)works out to 8, giving you 8 % 7, so $numberwill be 1, which is the remainder of 8/7.

在您发布的示例中,(3 - 2 + 7)结果为 8,为您提供8 % 7,因此$number将为1,即 8/7 的剩余部分。

回答by Pascal MARTIN

It is the modulus operator:

它是模运算符

$a % $b= Remainder of $adivided by $b.

$a % $b=$a除以 的余数$b

It is often used to get "one element every N elements". For instance, to only get one element each three elements:

它通常用于获取“每 N 个元素一个元素”。例如,每三个元素只获取一个元素:

for ($i=0 ; $i<10 ; $i++) {
    if ($i % 3 === 0) {
        echo $i . '<br />';
    }
}

Which gets this output:

得到这个输出:

0
3
6
9

(Yeah, OK, $i+=3would have done the trick; but this was just a demo.)

(是的,好吧,$i+=3本来可以做到的;但这只是一个演示。)

回答by Dana the Sane

It is the modulusoperator. In the statement $a % $bthe result is the remainder when $ais divided by $b

它是运算符。在语句中$a % $b,结果$a是除以的余数$b

回答by kredo900

Using this operator one can easily calculate odd or even days in month for example, if needed for schedule or something:

使用这个运算符可以很容易地计算一个月中的奇数或偶数天,例如,如果需要时间表或其他东西:

<?php echo (date(j) % 2 == 0) ? 'Today is even date' : 'Today is odd date'; ?>

回答by php_coder_3809625

%means modulus.

%表示模数。

Modulus is the fancy name for "remainder after divide" in mathematics.

模数是数学中“除法后的余数”的奇特名称。

(numerator) mod (denominator) = (remainder)

In PHP

在 PHP 中

<?php
    $n = 13;
    $d = 7
    $r = "$n % $d";
    echo "$r is ($n mod $d).";
?>

In this case, this script will echo

在这种情况下,此脚本将回显

6 is (13 mod 7).

Where $ris for the remainder (answer), $nfor the numerator and $dfor the denominator. The modulus operator is commonly used in public-key cryptographydue to its special characteristic as a one-way function.

哪里$r是余数(答案)、$n分子和$d分母。模运算符因其作为单向函数的特殊特性而常用于公钥密码学

回答by fyrye

Since so many people say "modulus finds the remainder of the divisor", let's start by defining exactly what a remainder is.

既然很多人都说“模数求除数的余数”,那么让我们从准确定义余数开始吧。

In mathematics, the remainder is the amount "left over" after performing some computation. In arithmetic, the remainder is the integer"left over" after dividing one integer by another to produce an integer quotient (integer division).

在数学中,余数是执行一些计算后“剩余”的数量。在算术,其余是 整数“遗留”除以一个整数由另一个,以产生一个整数商(整数除法)之后。

See: http://en.wikipedia.org/wiki/Remainder

请参阅:http: //en.wikipedia.org/wiki/Remainder

So %(integer modulus) is a simple way of asking, "How much of the divisor is left over after dividing?"

所以%(整数模数)是一种简单的提问方式,“除法后还剩下多少除数?”

To use the OP's computation of (3 - 2 + 7) = 8 % 7 = 1:

要使用 OP 的计算(3 - 2 + 7) = 8 % 7 = 1

It can be broken down into:

它可以分解为:

(3 - 2 + 7) = 8
8 / 7 = 1.143 #Rounded up
.143 * 7 = 1.001 #Which results in an integer of 1

7 can go into 8 1 time with .14 of 7 leftover

(3 - 2 + 7) = 8
8 / 7 = 1.143 #Rounded up
.143 * 7 = 1.001 #Which results in an integer of 1

7 可以进入 8 1 次,剩余 7 中的 0.14

That's all there is to it. I hope this helps to simplify how exactly modulus works.

这里的所有都是它的。我希望这有助于简化模数的工作原理。



Additional examples using different divisors with 21.

使用不同除数与 21 的其他示例。

Breakdown of 21 % 3 = 0:

21 / 3  = 7.0
3 * 0 = 0

(3 can go into 21 7 times with 0 of 3 leftover)

细分21 % 3 = 0

21 / 3  = 7.0
3 * 0 = 0

(3 可以进入 21 7 次,剩余 3 个中的 0 个)

Breakdown of 21 % 6 = 3:

21 / 6 = 3.5
.5 * 6 = 3

(6 can go into 21 3 times with .5 of 6 leftover)

细分21 % 6 = 3

21 / 6 = 3.5
.5 * 6 = 3

(6 可以进入 21 3 次,剩余的 6 次为 0.5)

Breakdown of 21 % 8 = 5:

21 / 8 = 2.625
.625 * 8 = 5

(8 can go into 21 2 times with .625 of 8 leftover)

细分21 % 8 = 5

21 / 8 = 2.625
.625 * 8 = 5

(8 可以进入 21 2 次,剩余 8 的 0.625)