溫馨提示×

怎樣在php中計(jì)算numeric數(shù)據(jù)

PHP
小樊
81
2024-09-22 00:23:38
欄目: 編程語言

在PHP中,可以使用內(nèi)置的函數(shù)來計(jì)算numeric數(shù)據(jù)。這里有一些常用的函數(shù):

  1. 加法:$sum = $a + $b;
  2. 減法:$difference = $a - $b;
  3. 乘法:$product = $a * $b;
  4. 除法:$quotient = $a / $b;
  5. 取模(求余數(shù)):$remainder = $a % $b;
  6. 冪運(yùn)算:$power = pow($a, $b);
  7. 開方:$sqrt = sqrt($a);
  8. 三角函數(shù):$sin = sin($a);(其中$a是以弧度為單位的角度)
  9. 對數(shù):$log = log($a);(其中$a是要計(jì)算對數(shù)的正實(shí)數(shù))

示例代碼:

<?php
// 加法
$sum = 3 + 4;
echo "3 + 4 = " . $sum . PHP_EOL;

// 減法
$difference = 10 - 2;
echo "10 - 2 = " . $difference . PHP_EOL;

// 乘法
$product = 5 * 6;
echo "5 * 6 = " . $product . PHP_EOL;

// 除法
$quotient = 12 / 4;
echo "12 / 4 = " . $quotient . PHP_EOL;

// 取模(求余數(shù))
$remainder = 7 % 3;
echo "7 % 3 = " . $remainder . PHP_EOL;

// 冪運(yùn)算
$power = pow(2, 3);
echo "2^3 = " . $power . PHP_EOL;

// 開方
$sqrt = sqrt(9);
echo "sqrt(9) = " . $sqrt . PHP_EOL;

// 三角函數(shù)(以弧度為單位的角度)
$sin = sin(M_PI / 2);
echo "sin(pi/2) = " . $sin . PHP_EOL;

// 對數(shù)
$log = log(8, 2);
echo "log_2(8) = " . $log . PHP_EOL;
?>

這個(gè)示例代碼展示了如何在PHP中使用內(nèi)置函數(shù)計(jì)算numeric數(shù)據(jù)。注意,M_PI常量需要使用math.h頭文件并在代碼開始處使用use math\M_PI;引入。

0