Php 简明教程

PHP - Arithmetic Operators Examples

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

In PHP, arithmetic operators are used to perform mathematical operations on numeric values. The following table highligts the arithmetic operators that are supported by PHP. Assume variable "$a" holds 42 and variable "$b" holds 20 −

Operator

Description

Example

+

Adds two operands

$a + $b = 62

-

Subtracts the second operand from the first

$a - $b = 22

*

Multiply both the operands

$a * $b = 840

/

Divide the numerator by the denominator

$a / $b = 2.1

%

Modulus Operator and remainder of after an integer division

$a % $b = 2

++

Increment operator, increases integer value by one

$a ++ = 43

 — 

Decrement operator, decreases integer value by one

$a — = 42

Example

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

The following example shows how you can use these arithmetic operators in 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

It will produce the following 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