Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the percent sign mean in PHP?

What exactly does this mean?

$number = ( 3 - 2 + 7 ) % 7; 
like image 368
Andrew Avatar asked Dec 19 '09 21:12

Andrew


People also ask

What does the percentage sign mean in coding?

JavaScript has many operators. One of them is the percent sign: % . It has a special meaning in JavaScript: it's the remainder operator. It obtains the remainder between two numbers. This is different from languages like Java, where % is the modulo operator.

What Is percent sign JavaScript?

The % operator is one of the "Arithmetic Operators" in JavaScript, like / , * , + , and - . The % operator returns the remainder of two numbers. It is useful for detecting even/odd numbers (like to make stripes) and for restricting a value to a range (like to wrapping an animated ball around) .


1 Answers

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

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

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

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

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

like image 121
zombat Avatar answered Sep 20 '22 04:09

zombat