Detailed explanation of arithmetic operators in php

怪我咯
Release: 2023-03-10 21:24:01
Original
1672 people have browsed it

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
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!