In this tutorial, you shall learn about Modulus Assignment Operator, its syntax, and how to use this operator in programs, with examples.

PHP – Modulus Assignment Operator

Modulus assignment operator is used to divide first operand by second operand, and assign the remainder in this division operation to the first operand.

Syntax

The syntax of using Modulus Assignment Operator is

</>
Copy
operand1 %= operand2

The operation would be similar to the following statement.

</>
Copy
operand1 = operand1 % operand2

which translates to the meaning of finding the value of operand1 % operand2 and assigning the result to operand1.

We can use variable for operand1; variable or value for operand2.

Example

In the following example, we use Modulus Assignment Operator to modulus assign $x with 10.

PHP Program

</>
Copy
<?php
$x = 42;
$x %= 10;
print_r("After modulus assignment, x is {$x}.");
?>

Output

PHP - Modulus Assignment Operator

Conclusion

In this PHP Tutorial, we learned about Modulus Assignment Operator, and how to assign the variable with the remainder in the division of the variable by a given value.