PHP 數學函數

WBOY
發布: 2024-08-29 12:46:37
原創
505 人瀏覽過

在本文中,我們將討論 PHP 數學函數。 PHP 代表超文本預處理器。 PHP 是一種程式語言,可用於建立簡單表單等小型應用程式到大型企業應用程式。它是一種基於伺服器端的腳本語言。每種程式語言都有許多內建的預設功能。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

這些功能可以幫助開發者快速編寫所需的程式碼。這些內建功能包含根據我們的要求所需的邏輯。這些內建功能可能是基於字串的功能、基於陣列的功能、基於數字的功能、基於 JSON 的功能等

現在讓我們來看看什麼是 PHP 數學函數

PHP 數學函數

它們是 PHP 作為程式語言的內建功能。這些函數的基本功能是提供一種機制,開發人員可以在其中進行某種數學計算或類似的操作。這些提供了快速的開發實踐,而無需編寫很長的程式碼。現在讓我們知道這些 PHP 數學函數的範圍

PHP 數學函數的範圍

這些 php 數學函數的範圍是整數和浮點型別。對於 32 位元計算機,PHP 中整數資料類型的範圍是 -2,147,483,647 到 2,147,483,647。任何小於 -2,147,483,647 的數字或任何大於 2,147,483,647 的數字或任何小於 -2,147,483,647 的數字都被視為浮點數。

現在我們將嘗試理解不同的 PHP 數學函數,及其使用範例:

1. abs() 函數

它是在 PHP 4+ 版本中引入的。它傳回數字的絕對值。函數的傳回類型是浮點數或整數,取決於函數中傳遞的參數類型。

範例

<!DOCTYPE html>
<html>
<body>
<?php
echo(abs(3.5) . "<br>");
echo(abs(-3.5) . "<br>");
echo(abs(5) . "<br>");
echo(abs(-5));
?>
</body>
</html>
登入後複製

輸出:

PHP 數學函數

2. acos() 函數

它是在 PHP 4+ 版本中引入的。它期望參數的範圍為 -1 到 +1。如果參數中傳遞的數字超出指定範圍,則傳回 NaN,否則傳回該數字的反餘弦值。函數的回傳類型是數字的反餘弦

範例

<!DOCTYPE html>
<html>
<body>
<?php
echo(acos(0.35) . "<br>");
echo(acos(-0.35) . "<br>");
echo(acos(5) . "<br>");
echo(acos(0.7253));
?>
</body>
</html>
登入後複製

輸出:

PHP 數學函數

3. asin() 函數

它是在 PHP 4+ 版本中引入的。它期望參數的範圍為 -1 到 +1。如果參數中傳遞的數字超出指定範圍,則傳回 NaN,否則傳回該數字的反正弦值。函數的回傳類型是數字的反正弦

範例

<!DOCTYPE html>
<html>
<body>
<?php
echo(asin(0.35) . "<br>");
echo(asin(-0.35) . "<br>");
echo(asin(5) . "<br>");
echo(asin(0.7253));
?>
</body>
</html>
登入後複製

輸出:

PHP 數學函數

4. ceil() 函數

它是在 PHP 4+ 版本中引入的。它將數字向上捨入到最接近的整數。例如,3.2 的 ceil 將為 4。它以最接近整數的形式傳回大於傳遞的參數

的整數

範例

<!DOCTYPE html>
<html>
<body>
<?php
echo(ceil(3.35) . "<br>");
echo(ceil(-4.35) . "<br>");
echo(ceil(5) . "<br>");
echo(ceil(14.8114700666069059));
?>
</body>
</html>
登入後複製

輸出:

PHP 數學函數

5.地板()函數

它是在 PHP 4+ 版本中引入的。它將數字向下捨去到最接近的整數。例如,3.2 的下限將為 3。它以最接近整數的形式傳回小於傳遞的參數

的整數

範例

<!DOCTYPE html>
<html>
<body>
<?php
echo(floor(3.35) . "<br>");
echo(floor(-2.35) . "<br>");
echo(floor(5) . "<br>");
echo(floor(14.811470062));
?>
</body>
</html>
登入後複製

輸出:

PHP 數學函數

6. pi() 函數

它是在 PHP 4+ 版本中引入的。它傳回 PI 的值,傳回類型是 float。

範例

<!DOCTYPE html>
<html>
<body>
<?php
echo(pi() . "<br>");
?>
</body>
</html>
登入後複製

輸出:

PHP 數學函數

7. pow() 函數

它是在 PHP 4+ 版本中引入的。它接受兩個參數,即 x 和 y。它計算 x 的 y 次方。它的回傳類型是整數或浮點數,這取決於參數的性質

範例

<!DOCTYPE html>
<html>
<body>
<?php
echo(pow(2,3) . "<br>");
echo(pow(2,4) . "<br>");
echo(pow(5,6) . "<br>");
echo(pow(3,5));
?>
</body>
</html>
登入後複製

輸出:

PHP 數學函數

8. log() function

It was introduced in PHP 4+ version. It accepts two arguments say x and y where x is a number and y is the logarithm of a number to base. If y is not passed then the default value ‘e’ is assumed. Its return type is float

Example:

<!DOCTYPE html>
<html>
<body>
<?php
echo(log(2.718) . "<br>");
echo(log(2) . "<br>");
echo(log(1) . "<br>");
echo(log(0));
?>
</body>
</html>
登入後複製

Output:

PHP 數學函數

9. log10() function

It was introduced in PHP 4+ version. It accepts one argument says x where x is a number whose base 10 logarithm needs to be calculated. Its return type is float

Example:

<!DOCTYPE html>
<html>
<body>
<?php
echo(log10(656) . "<br>");
echo(log10(455) . "<br>");
echo(log10(145) . "<br>");
?>
</body>
</html>
登入後複製

Output:

PHP 數學函數

10. round() function

It was introduced in PHP 4+ version. It rounds a number. It expects three parameters where the first parameter is number, the second parameter is for precision and the third argument is for mode. The only first argument is mandatory

Example:

<!DOCTYPE html>
<html>
<body>
<?php
echo(round(3.35) . "<br>");
echo(round(-2.35) . "<br>");
echo(round(5) . "<br>");
?>
</body>
</html>
登入後複製

Output:

PHP 數學函數

Apart from specified PHP math functions, there are several other math functions that could be used as per our requirements. We could run above described above-mentioned PHP code snippet to execute it

Let us know how to run an example code snippet.

  1. Install php5 or 5+
  2. Install localhost say WampServer etc
  3. Now create a file and name it as index.php
  4. Paste example snippet in the file created
  5. Run localhost and run index.php on browser

Conclusion

Php is a very vast programming language, we can learn it in order to make web applications. It is used to handle server-side scripting logic. Although we could insert our HTML code also within PHP as we have used in example snippets.

Php has a large database for inbuilt functionalities. There are several functionalities that use string as a parameter, other functionalities use an array. These inbuilt functionalities help us to solve our requirements without writing much code.

以上是PHP 數學函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!