


A large summary of PHP mathematical operation functions (classics worth collecting), PHP worth collecting_PHP tutorial
Jul 12, 2016 am 08:55 AMA large summary of PHP mathematical operation functions (classics worth collecting), PHP is worth collecting
This article summarizes and analyzes PHP mathematical operation functions. Share it with everyone for your reference, the details are as follows:
1. Description of commonly used functions:
Abs: Get the absolute value.
Acos: Get the arc cosine value.
Asin: Get the arcsine value.
Atan: Get the arctangent value.
Atan2: Calculate the arc tangent of a two number.
base_convert: The carry method for converting numbers.
BinDec: Convert binary to decimal.
Ceil: Calculate the smallest integer greater than the specified number.
Cos: Cosine calculation.
DecBin: Convert decimal to binary.
DecHex: Convert decimal to hexadecimal.
DecOct: Convert decimal to octal.
Exp: The power of the natural logarithm e.
Floor: Calculate the largest integer less than the specified number.
getrandmax: the maximum value of random numbers.
HexDec: Convert hexadecimal to decimal.
Log: Natural logarithm value.
Log10: Log value of 10 base.
max: Get the maximum value.
min: Get the minimum value.
mt_rand: Get a random value.
mt_srand: Configure random number seed.
mt_getrandmax: The maximum value of random number.
number_format: Format number string.
OctDec: Convert octal to decimal.
pi: Pi.
pow: power.
rand: Get a random value.
round: Rounded.
Sin: Sine calculation.
Sqrt: Square root.
srand: configure random number seed.
Tan: Tangent calculation.
2. Function analysis is as follows:
Abs
Get the absolute value.
Syntax: mixed abs(mixed number);
Return value: Mixed type data
Function type: Mathematical operation
Content description: Returns the absolute value of parameter number. If number is a multiple-precision floating-point number, the return value is also a multiple-precision floating-point number; for other types, the return type is an integer.
Acos
Get the arc cosine value.
Syntax: float acos(float arg);
Return value: floating point number
Function type: Mathematical operation
Content description: Returns the arc cosine of parameter arg.
Reference: asin() atan()
Asin
Get the arcsine value.
Syntax: float asin(float arg);
Return value: floating point number
Function type: Mathematical operation
Content description: Returns the arc sine value (arc sine) of parameter arg.
Reference: acos() atan()
Atan
Get the arctangent value.
Syntax: float atan(float arg);
Return value: floating point number
Function type: Mathematical operation
Content description: Returns the arc tangent of parameter arg (arc tangent).
Reference: acos() asin()
Atan2
Calculate the arctangent of a two number.
Syntax: float atan2(float y, float x);
Return value: floating point number
Function type: Mathematical operation
Content description: This function is used to calculate the arc tangent of two numbers y and x. The calculation result is similar to atan()'s calculation of y/x, but this function will affect the result due to the positive and negative signs of x and y. The return value of this function must fall between positive and negative pi (-PI <= (value) ≶= PI)
Reference acos() atan() asin()
base_convert
Convert the carry method of numbers.
Syntax: string base_convert(string number, int frombase, int tobase);
Return value: string
Function type: Mathematical operation
Content Description: This function converts the numeric string number from carry from base to carry from tobase. This formula can handle carry modes from binary to hexadecimal. Before the decimal place, it is represented by numbers, and after the decimal place is exceeded, it is represented by English letters. For example, the sequence of hexadecimal single digits is 123456789abcdef, and the sequence of 10 is the seventeenth, and then one digit is carried forward. In hexadecimal a is the tenth, b is the eleventh, z is the thirty-sixth, and 10 is the thirty-seventh, then the carry is carried out.
Usage Example
This example converts a hexadecimal string into a binary string
<?php $binary = base_convert($hexadecimal, 16, 2); echo "十六進位字符串"$hexadecimal"轉(zhuǎn)成二進位為"$binary"。"; ?>
BinDec
Convert binary to decimal.
Syntax: int bindec(string binary_string);
Return value: integer
Function type: Mathematical operation
Content description: This function converts a binary digit string into a decimal integer. Since PHP uses 32-bit signed integers for calculations, the maximum decimal number it can handle is 2147483647, which is 1111111111111111111111111111111 (31 ones) in binary.
Reference: DecBin()
Ceil
Computes the smallest integer greater than the specified number.
Syntax: int ceil(float number);
Return value: integer
Function type: Mathematical operation
Content description: This function is used to calculate the smallest integer larger than the floating point parameter number.
Usage Example
The return value in this example is 4.
<?php $nextint=ceil(3.14); echo $nextint; ?>
Reference: Floor() round()
Cos
余弦計算。
語法: float cos(float arg);
返回值: 浮點數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)計算參數(shù) arg 的余弦值 (cosine)。
參考: Sin() Tan()
DecBin
十進位轉(zhuǎn)二進位。
語法: string decbin(int number);
返回值: 字符串
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)將十進位數(shù)字轉(zhuǎn)成二進位字符串。由于 PHP 使用 32 位有正負號整數(shù)計算,能處理的最大十進位數(shù)字為 2147483647,也就是二進位數(shù)字的 1111111111111111111111111111111 (31 個 1)。
參考: BinDec()
DecHex
十進位轉(zhuǎn)十六進位。
語法: string dechex(int number);
返回值: 字符串
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)將十進位數(shù)字轉(zhuǎn)成十六進位字符串。由于 PHP 使用 32 位有正負號整數(shù)計算,能處理的最大十進位數(shù)字為 2147483647,也就是十六進位數(shù)字 7fffffff。
參考: HexDec()
DecOct
十進位轉(zhuǎn)八進位。
語法: string decoct(int number);
返回值: 字符串
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)將十進位數(shù)字轉(zhuǎn)成八進位字符串。由于 PHP 使用 32 位有正負號整數(shù)計算,能處理的最大十進位數(shù)字為 2147483647,也就是八進位數(shù)字 17777777777。
參考: OctDec()
Exp
自然對數(shù) e 的次方值。
語法: float exp(float arg);
返回值: 浮點數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)計算自然對數(shù) (natural logarithm) 的 arg 次方值。
參考: pow() Log()
Floor
計算小于指定數(shù)的最大整數(shù)。
語法: int floor(float number);
返回值: 整數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)用來計算比浮點參數(shù) number 小的最大整數(shù)。
使用范例
本例返回值為 3。
<?php $lastint=floor(3.14); echo $lastint; ?>
參考: Ceil() round()
getrandmax
亂數(shù)的最大值。
語法: int getrandmax(void);
返回值: 整數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)計算亂數(shù)函數(shù) rand() 可能取得的取得的最大亂數(shù)值。本函數(shù)不需參數(shù)。
參考: rand() srand() mt_rand() mt_srand() mt_getrandmax()
HexDec
十六進位轉(zhuǎn)十進位。
語法: int hexdec(string hex_string);
返回值: 整數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)將十六進位字符串轉(zhuǎn)成十進位數(shù)字。由于 PHP 使用 32 位有正負號整數(shù)計算,能處理的最大十六進位數(shù)字為 7fffffff,也就是十進位數(shù)字的 2147483647。
參考: DecHex()
Log
自然對數(shù)值。
語法: float log(float arg);
返回值: 浮點數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)計算參數(shù) arg 的自然對數(shù) (natural logarithm) 值。
Log10
10 基底的對數(shù)值。
語法: float log10(float arg);
返回值: 浮點數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)計算參數(shù) arg 的 10 基底對數(shù)值。
max
取得最大值。
語法: mixed max(mixed arg1, mixed arg2 .... mixed argn);
返回值: 混合類型資料
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)計算參數(shù)間的最大值。若第一個參數(shù)是數(shù)字數(shù)組,則會找出該數(shù)組的最大數(shù)字。若第一個參數(shù)非數(shù)組,則需二個以上的參數(shù)。這些數(shù)字可以是整數(shù)、倍精確數(shù)或數(shù)字字符串的類型。參數(shù)的數(shù)目不限,視用戶的需求而定。在計算時,只要有一個參數(shù)是倍精確數(shù),本函數(shù)會將所有的參數(shù)都轉(zhuǎn)成倍精確數(shù),并返回倍精確數(shù)。若參數(shù)只有整數(shù)及數(shù)字字符串,則會將所有的參數(shù)轉(zhuǎn)換成整數(shù),并返回整數(shù)。
參考: min()
min
取得最小值。
語法: mixed min(mixed arg1, mixed arg2 .... mixed argn);
返回值: 混合類型資料
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)計算參數(shù)間的最小值。若第一個參數(shù)是數(shù)字數(shù)組,則會找出該數(shù)組的最小數(shù)字。若第一個參數(shù)非數(shù)組,則需二個以上的參數(shù)。這些數(shù)字可以是整數(shù)、倍精確數(shù)或數(shù)字字符串的類型。參數(shù)的數(shù)目不限,視用戶的需求而定。在計算時,只要有一個參數(shù)是倍精確數(shù),本函數(shù)會將所有的參數(shù)都轉(zhuǎn)成倍精確數(shù),并返回倍精確數(shù)。若參數(shù)只有整數(shù)及數(shù)字字符串,則會將所有的參數(shù)轉(zhuǎn)換成整數(shù),并返回整數(shù)。
參考: max()
mt_rand
取得亂數(shù)值。
語法: int mt_rand([int min], [int max]);
返回值: 整數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)不使用一般常用的 libc 來計算亂數(shù)值,而是使用計算速度至少快四倍的馬其賽特旋轉(zhuǎn) (Mersenne Twister) 演算法來計算亂數(shù)值。有關(guān)馬特賽特旋轉(zhuǎn)演算法可在松本真的 http://www.math.keio.ac.jp/~matumoto/emt.html 找到更多的相關(guān)信息,最佳化的原始程序則在 http://www.scp.syr.edu/~marc/hawk/twister.html。若沒有指定亂數(shù)的最大及最小范圍,本函數(shù)會自動的從 0 到 RAND_MAX 中取一個亂數(shù)。若有指定 min 及 max 的參數(shù),則從指定參數(shù)中取一個數(shù)字,例如 mt_rand(38, 49) 則會從 38 到 49 之間取一個亂數(shù)值。值得注意的是為使亂數(shù)的亂度最大,每次在取亂數(shù)之前最好使用 mt_srand() 以配置新的亂數(shù)種子。
參考: rand() srand() getrandmax() mt_srand() mt_getrandmax()
mt_srand
配置亂數(shù)種子。
語法: void mt_srand(int seed);
返回值: 無
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)傳入?yún)?shù) seed 后,配置亂數(shù)的種子。值得注意的是參數(shù) seed 值最好也是隨機出現(xiàn)的數(shù)字,例如利用加入時間做為變量的來源就是不錯的方法,或者開發(fā)其它的硬體周邊界面可取得更好的亂數(shù)。
使用范例
本例加入時間的因素,以執(zhí)行時的百萬分之一秒當(dāng)亂數(shù)種子
<?php mt_srand((double)microtime()*1000000); $randval = mt_rand(); echo $randval; ?>
參考: rand() srand() getrandmax() mt_rand() mt_getrandmax()
mt_getrandmax
亂數(shù)的最大值。
語法: int mt_getrandmax(void);
返回值: 整數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)計算亂數(shù)函數(shù) mt_rand() 可能取得的取得的最大亂數(shù)值。本函數(shù)不需參數(shù)。
參考: rand() srand() getrandmax() mt_srand() mt_rand()
number_format
格式化數(shù)字字符串。
語法: string number_format(float number, int [decimals], string [dec_point], string [thousands_sep]);
返回值: 字符串
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)用來將浮點參數(shù) number 格式化。若沒加參數(shù) decimals 則返回的字符串只要整數(shù)部份,加了此參數(shù)才依參數(shù)指定的小數(shù)點位數(shù)返回。參數(shù) dec_point 表示小數(shù)點的表示方式方法,默認值是 ".",若需要轉(zhuǎn)換成其它的小數(shù)點就可以在這個參數(shù)改掉。參數(shù) thousands_sep 為整數(shù)部份每三位的分隔符號,默認值是 ","。本函數(shù)最特別的地方就是參數(shù)數(shù)目,最少要有一個,也就是欲格式化的字符串;也可以有二個或者四個參數(shù),但不能用三個參數(shù)。值得注意的是指定小數(shù)點的位數(shù)之后的數(shù)字直接舍棄,沒有四舍五入的情形。
使用范例
<?php $short_pi = "3.14159"; $my_pi = number_format($short_pi, 2); echo $my_pi."n"; // 3.14 $foo = 850017.9021; $new_foo = number_format($foo, 3, ".", " "); echo $new_foo."n"; // 850 017.902 ?>
OctDec
八進位轉(zhuǎn)十進位。
語法: string decoct(int number);
返回值: 字符串
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)將八進位字符串轉(zhuǎn)成十進位數(shù)字。由于 PHP 使用 32 位有正負號整數(shù)計算,能處理最大的八進位數(shù)字為 17777777777,也就是十進位數(shù)字的 2147483647。
參考: DecOct()
pi
圓周率。
語法: double pi(void);
返回值: 倍精確數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)返回圓周率。不需輸入?yún)?shù)。
pow
次方。
語法: float pow(float base, float exp);
返回值: 浮點數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)計算次方值。參數(shù) base 為基底,exp 為冪數(shù)。
使用范例
<?php print(pow(2,3)); // 8 print(pow(5,4)); // 625 ?>
參考: Log10() Exp()
rand
取得隨機數(shù)值。
語法: int rand([int min], [int max]);
返回值: 整數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)用來取得隨機數(shù)值。若沒有指定隨機數(shù)的最大及最小范圍,本函數(shù)會自動的從 0 到 RAND_MAX 中取一個隨機數(shù)。若有指定 min 及 max 的參數(shù),則從指定參數(shù)中取一個數(shù)字。例如 rand(38, 49) 則會從 38 到 49 之間取一個隨機數(shù)值,UNIX 系統(tǒng)包含 49、Win32 系統(tǒng)不包含 49 ([email=zkimmel@earthlink.net]zkimmel@earthlink.net[/email] 10-May-1999)。值得注意的是為使隨機數(shù)的亂度最大,每次在取隨機數(shù)之前最好使用 srand() 以配置新的隨機數(shù)種子。
參考: srand() getrandmax() mt_rand() mt_srand() mt_getrandmax()
round
四舍五入。
語法: double round(double val);
返回值: 倍精確數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)用來將數(shù)字小數(shù)點后四舍五入。
使用范例
<?php $foo1 = round(3.4); $foo2 = round(3.5); $foo3 = round(3.6); echo "round(3.4): ".$foo1."<br>n"; echo "round(3.5): ".$foo2."<br>n"; echo "round(3.6): ".$foo3; ?>
參考: Ceil() Floor()
Sin
正弦計算。
語法: float sin(float arg);
返回值: 浮點數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)計算參數(shù) arg 的正弦值 (sine)。
參考: Cos() Tan()
Sqrt
開平方根。
語法: float sqrt(float arg);
返回值: 浮點數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)將參數(shù) arg 開平方根。
srand
配置隨機數(shù)種子。
語法: void srand(int seed);
返回值: 無
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)傳入?yún)?shù) seed 后,配置隨機數(shù)的種子。值得注意的是參數(shù) seed 值最好也是隨機出現(xiàn)的數(shù)字,例如利用加入時間做為變量的來源就是不錯的方法,或者開發(fā)其它的硬體周邊界面可取得更好的隨機數(shù)。
使用范例
本例加入時間的因素,以執(zhí)行時的百萬分之一秒當(dāng)隨機數(shù)種子
<?php srand((double)microtime()*1000000); $randval = rand(); echo $randval; ?>
參考: rand() getrandmax() mt_srand() mt_rand() mt_getrandmax()
Tan
正切計算。
語法: float tan(float arg);
返回值: 浮點數(shù)
函數(shù)種類: 數(shù)學(xué)運算
內(nèi)容說明: 本函數(shù)計算參數(shù) arg 的正切值 (tangent)。
參考: Sin() Cos()
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:
- PHP數(shù)組函數(shù)array_multisort()用法實例分析
- PHP計算數(shù)組中值的和與乘積的方法(array_sum與array_product函數(shù))
- 詳解PHP匿名函數(shù)與注意事項
- PHP常見數(shù)組函數(shù)用法小結(jié)
- PHP匿名函數(shù)和use子句用法實例
- 簡單談?wù)凱HP中strlen 函數(shù)
- PHP中array_keys和array_unique函數(shù)源碼的分析
- PHP查詢并刪除數(shù)據(jù)庫多列重復(fù)數(shù)據(jù)的方法(利用數(shù)組函數(shù)實現(xiàn))
- php強大的時間轉(zhuǎn)換函數(shù)strtotime
- PHP函數(shù)超時處理方法
- php的閉包(Closure)匿名函數(shù)初探
- PHP函數(shù)import_request_variables()用法分析

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.
