Home  >  Article  >  Backend Development  >  php arithmetic operators

php arithmetic operators

伊谢尔伦
伊谢尔伦Original
2016-11-24 13:37:051119browse

Remember the basic math knowledge you learned in school? Just like them.

Arithmetic Operator

Example Name Result

-$a Negative value of $a.

$a + $b Addition The sum of $a and $b.

$a - $b Subtraction The difference between $a and $b.

$a * $b Multiplication The product of $a and $b.

$a / $b Division quotient of $a divided by $b.

$a % $b Modulo The remainder of $a divided by $b.

The division operator always returns a floating point number. The only exception is that both operands are integers (or integers converted from strings) and are exactly divisible, in which case it returns an integer.

The operands of the modulo operator will be converted to integers (except for the decimal part) before operation.

The result of the modulo operator % is the same as the sign (positive or negative sign) of the dividend. That is, the result of $a % $b has the same sign as $a. For example:

<?php
echo (5 % 3)."\n";           // prints 2
echo (5 % -3)."\n";          // prints 2
echo (-5 % 3)."\n";          // prints -2
echo (-5 % -3)."\n";         // prints -2
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php assignment operatorNext article:php assignment operator