Php 简明教程

PHP - Arithmetic Operators Examples

在 PHP 中,算术运算符用于对数字值执行数学运算。下表重点介绍了 PHP 支持的算术运算符。假设变量“$a”的值为 42,而变量“$b”的值为 20 −

Operator

Description

Example

+

Adds two operands

$a + $b = 62

-

从第一个操作数中减去第二个操作数

$a - $b = 22

*

Multiply both the operands

$a * $b = 840

/

将分子除以分母

$a / $b = 2.1

%

模运算符,整数除法后的余数

$a % $b = 2

++

自增运算符,将整数加一

$a ++ = 43

 — 

自减运算符,将整数减一

$a — = 42

Example

以下示例说明了如何在 PHP 中使用这些算术运算符 −

<?php
   $a = 42;
   $b = 20;

   $c = $a + $b;
   echo "Addtion Operation Result: $c \n";

   $c = $a - $b;
   echo "Substraction Operation Result: $c \n";

   $c = $a * $b;
   echo "Multiplication Operation Result: $c \n";

   $c = $a / $b;
   echo "Division Operation Result: $c \n";

   $c = $a % $b;
   echo "Modulus Operation Result: $c \n";

   $c = $a++;
   echo "Increment Operation Result: $c \n";

   $c = $a--;
   echo "Decrement Operation Result: $c";
?>

它将生成以下 output

Addtion Operation Result: 62
Substraction Operation Result: 22
Multiplication Operation Result: 840
Division Operation Result: 2.1
Modulus Operation Result: 2
Increment Operation Result: 42
Decrement Operation Result: 43