Home  >  Article  >  Backend Development  >  Detailed explanation of arithmetic operators in php

Detailed explanation of arithmetic operators in php

怪我咯
怪我咯Original
2017-06-20 11:33:231683browse

Do you still remember the basic mathematics knowledge you learned in school? The arithmetic operators in php are just like them.

Example Name Result
-$ a negates the negative value of .
$a + $b Addition The sum of the sum.
$a - $b Subtraction The difference between the sum.
$a * $b The product of the multiplication sum.
$a / $b Division Quotient of division.
$a % $b takes the remainder modulo .

The division operator always returns a floating point number. The only exception is the following: both operands are integers (or stringsconverted to integers) 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
?>

The above is the detailed content of Detailed explanation of arithmetic operators in php. For more information, please follow other related articles on the PHP Chinese website!

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