PHP mathematical function summary (classic worthy of favorites), php worthy of favorites _ PHP Tutorial

Source: Internet
Author: User
Tags acos asin binary to decimal decimal to binary float number natural logarithm random seed
PHP mathematical function summary (classic worthy of favorites), php worthy of favorites. PHP mathematical operation functions (classic worthy of favorites), php worthy of favorites This article summarizes and analyzes the PHP mathematical operation functions. For your reference, I. common functions: PHP mathematical functions (classic functions worth collecting) and php functions worth collecting

This article summarizes and analyzes PHP mathematical functions. We will share this with you for your reference. The details are as follows:

1. common functions:

Abs: obtain the absolute value.

Acos: returns the arc cosine.

Asin: returns the arc sine.

Atan: Get the arc tangent value.

Atan2: calculate the arc tangent value of the second number.

Base_convert: convert the carry mode of numbers.

BinDec: binary to decimal.

Ceil: calculates the smallest integer greater than the specified number.

Cos: returns the cosine.

DecBin: decimal to binary.

DecHex: decimal to hexadecimal.

DecOct: decimal to octal.

Exp: the power of natural logarithm e.

Floor: calculates the maximum integer smaller than the specified number.

Getrandmax: maximum value of a random number.

HexDec: Hexadecimal to decimal.

Log: Natural logarithm value.

Log10: the logarithm of the base 10.

Max: obtain the maximum value.

Min: obtain the minimum value.

Mt_rand: returns a random value.

Mt_srand: configure the random seed.

Mt_getrandmax: maximum value of a random number.

Number_format: format a numeric string.

OctDec: turn octal to decimal.

Pi: circumference rate.

Pow: power.

Rand: returns a random number.

Round: rounding.

Sin: sine calculation.

Sqrt: Square root.

Srand: configure the random seed.

Tan: returns the tangent.

II. function analysis:

Abs
Obtain the absolute value.
Syntax: mixed abs (mixed number );
Returned value: Hybrid Data
Function type: mathematical operation
Description: returns the absolute value of the parameter number. If 'number' is a multiple-precision floating-point number, the return value is also a double floating-point number. if 'number' is other types, the return type is an integer.

Acos
Returns the arc cosine.
Syntax: float acos (float arg );
Return value: floating point number
Function type: mathematical operation
Description: returns the arc cosine of the arg parameter ).
Reference: asin () atan ()

Asin
Returns the arc sine.
Syntax: float asin (float arg );
Return value: floating point number
Function type: mathematical operation
Description: returns the arc sine of the arg parameter ).
Reference: acos () atan ()

Atan
Returns the arc tangent value.
Syntax: float atan (float arg );
Return value: floating point number
Function type: mathematical operation
Description: returns the arc tangent (arc tangent) of the arg parameter ).
Reference: acos () asin ()

Atan2
Calculate the arc tangent of the second number.
Syntax: float atan2 (float y, float x );
Return value: floating point number
Function type: mathematical operation
Description: This function is used to calculate the arc tangent values of two y and x. The calculation result is similar to that of y/x in atan, however, this function will affect the result because of the plus and minus signs of x and y. The return value of this function must fall between the positive and negative circle rates (-PI <= (value) between = PI)
Refer to acos () atan () asin ()

Base_convert
Converts the carry mode of numbers.
Syntax: string base_convert (string number, int frombase, int tobase );
Return value: string
Function type: mathematical operation
Description: This function converts a number string from frombase to tobase. This method can process the binary data to the hexadecimal data. The decimal place is represented by numbers before decimal places, and the decimal place is represented by English letters. For example, the hexadecimal single-digit sequence is 123456789 abcdef, and the order of 10 is 17th. The hexadecimal carry a is the tenth, B is 11th, z is 36th, and 10 is 37th.

Example

In this example, the hexadecimal string is converted into a binary string.

<? Php $ binary = base_convert ($ hexadecimal, 16, 2); echo "hexadecimal string" $ hexadecimal "to binary" $ binary ". ";?>

BinDec
Binary to decimal.
Syntax: int bindec (string binary_string );
Return value: integer
Function type: mathematical operation
Description: This function converts a binary numeric string to a decimal integer. Because PHP uses 32-bit positive and negative integers, the maximum decimal number to be processed is 2147483647, that is, 1111111111111111111111111111111 of binary numbers (31 1 ).
Reference: DecBin ()

Ceil
Calculates the smallest integer greater than the specified number.
Syntax: int ceil (float number );
Return value: integer
Function type: mathematical operation
Description: This function is used to calculate the smallest integer greater than the floating point parameter number.

Example

In this example, the return value is 4.

<?php$nextint=ceil(3.14);echo $nextint;?> 

Reference: Floor () round ()

Cos
Returns the cosine.
Syntax: float cos (float arg );
Return value: floating point number
Function type: mathematical operation
Description: returns the cosine of the arg parameter (cosine ).
Reference: Sin () Tan ()

DecBin
Decimal to binary.
Syntax: string decbin (int number );
Return value: string
Function type: mathematical operation
Description: This function converts decimal digits into binary strings. Because PHP uses 32-bit positive and negative integers, the maximum decimal number to be processed is 2147483647, that is, 1111111111111111111111111111111 of binary numbers (31 1 ).
Reference: BinDec ()

DecHex
Decimal to hexadecimal.
Syntax: string dechex (int number );
Return value: string
Function type: mathematical operation
Description: This function converts a decimal digit into a hexadecimal string. Because PHP uses 32-bit positive and negative integers, the maximum decimal number to be processed is 2147483647, that is, the hexadecimal number 7 fffff.
Reference: HexDec ()

DecOct
Decimal to octal.
Syntax: string decoct (int number );
Return value: string
Function type: mathematical operation
Description: This function converts decimal digits into octal strings. Since PHP uses 32-bit positive and negative integers, the maximum decimal number that can be processed is 2147483647, that is, the octal number 17777777777.
Reference: OctDec ()

Exp
The power of natural logarithm e.
Syntax: float exp (float arg );
Return value: floating point number
Function type: mathematical operation
Description: This function computes the arg power of the natural logarithm (natural logarithm.
Reference: pow () Log ()

Floor
Calculates the maximum integer that is less than the specified number.
Syntax: int floor (float number );
Return value: integer
Function type: mathematical operation
Description: This function is used to calculate the maximum integer smaller than the floating point parameter number.

Example

In this example, 3 is returned.

<?php$lastint=floor(3.14);echo $lastint;?> 

Reference: Ceil () round ()

Getrandmax
The maximum number of disconnections.
Syntax: int getrandmax (void );
Return value: integer
Function type: mathematical operation
Description: This function calculates the maximum number of random values that the rand () function may obtain. This function does not require parameters.
Reference: rand () srand () mt_rand () mt_srand () mt_getrandmax ()

HexDec
Hexadecimal to decimal.
Syntax: int hexdec (string hex_string );
Return value: integer
Function type: mathematical operation
Description: This function converts a hexadecimal string into a decimal number. Because PHP uses 32-bit positive and negative integers, the maximum hexadecimal number that can be processed is 7 fffffff, that is, 2147483647 of the decimal number.
Reference: DecHex ()

Log
Natural logarithm value.
Syntax: float log (float arg );
Return value: floating point number
Function type: mathematical operation
Description: This function calculates the natural logarithm (natural logarithm) value of the arg parameter.

Log10
10 base logarithm.
Syntax: float log10 (float arg );
Return value: floating point number
Function type: mathematical operation
Description: This function calculates the 10 base logarithm of the arg parameter.

Max
Obtain the maximum value.
Syntax: mixed max (mixed arg1, mixed arg2... mixed argn );
Returned value: Hybrid Data
Function type: mathematical operation
Description: maximum value between parameters of this function. If the first parameter is a number array, the maximum number of the array is displayed. If the first parameter is not an array, more than two parameters are required. These numbers can be integers, exact times, or numeric strings. The number of parameters is unlimited, depending on your needs. During calculation, as long as one parameter is a Times exact number, this function will convert all parameters to multiple exact numbers and return the times exact number. If the parameter only contains an integer and a numeric string, all parameters are converted to an integer and an integer is returned.
Reference: min ()

Min
Obtain the minimum value.
Syntax: mixed min (mixed arg1, mixed arg2... mixed argn );
Returned value: Hybrid Data
Function type: mathematical operation
Description: This function calculates the minimum value between parameters. If the first parameter is a number array, the minimum number of the array is displayed. If the first parameter is not an array, more than two parameters are required. These numbers can be integers, exact times, or numeric strings. The number of parameters is unlimited, depending on your needs. During calculation, as long as one parameter is a Times exact number, this function will convert all parameters to multiple exact numbers and return the times exact number. If the parameter only contains an integer and a numeric string, all parameters are converted to an integer and an integer is returned.
Reference: max ()

Mt_rand
Returns a random value.
Syntax: int mt_rand ([int min], [int max]);
Return value: integer
Function type: mathematical operation
Description: This function uses Mersenne Twister (Mersenne Twister) algorithm, which is at least four times faster than libc, to calculate random values. About the Matt Saite rotation algorithm available in the matterse real http://www.math.keio.ac.jp /~ Matumoto/emt.html find more relevant information, the optimized original program in the http://www.scp.syr.edu /~ Marc/hawk/twister.html. If the maximum and minimum range of the random number is not specified, this function automatically obtains a random number from 0 to RAND_MAX. If the min and max parameters are specified, a number is obtained from the specified parameters, for example, mt_rand (38, 49). a random value is obtained from 38 to 49. It is worth noting that mt_srand () is recommended to be used to configure a new random number before getting the random number.
Reference: rand () srand () getrandmax () mt_srand () mt_getrandmax ()

Mt_srand
Configure the random seed.
Syntax: void mt_srand (int seed );
Return value: None
Function type: mathematical operation
Description: after the seed parameter is input in this function, the seed of the random number is configured. It is worth noting that the seed value of the parameter is also a random number. for example, it is a good method to use the time added as the source of the variable, or you can develop other hardware peripheral interfaces to achieve better random numbers.

Example

In this example, the time factor is added to the execution time.

<?phpmt_srand((double)microtime()*1000000);$randval = mt_rand();echo $randval;?> 

Reference: rand () srand () getrandmax () mt_rand () mt_getrandmax ()

Mt_getrandmax
The maximum number of disconnections.
Syntax: int mt_getrandmax (void );
Return value: integer
Function type: mathematical operation
Description: This function calculates the maximum random value that mt_rand () may obtain. This function does not require parameters.
Reference: rand () srand () getrandmax () mt_srand () mt_rand ()

Number_format
Format the numeric string.
Syntax: string number_format (float number, int [decimals], string [dec_point], string [thousands_sep]);
Return value: string
Function type: mathematical operation
Description: This function is used to format the floating point parameter number. If the decimals parameter is not added, only the integer part of the returned string is added. this parameter is returned based on the number of decimal places specified by the parameter. The dec_point parameter represents the decimal point. The default value is ".". you can change it to another decimal point. The thousands_sep parameter is the separator number of each three digits in the integer part. the default value is ",". The most special part of this function is the number of parameters. there must be at least one, that is, the string to be formatted. there can also be two or four parameters, but not three parameters. It is worth noting that the number after the specified decimal point is discarded without rounding.

Example

<?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
Turn octal to decimal.
Syntax: string decoct (int number );
Return value: string
Function type: mathematical operation
Description: This function converts an octal string to a decimal number. Because PHP uses 32-bit positive and negative integers, it can process the maximum octal number as 17777777777, that is, 2147483647 of the decimal number.
Reference: DecOct ()

Pi
Circumference rate.
Syntax: double pi (void );
Return value: exact number of times
Function type: mathematical operation
Description: This function returns the circumference rate. No parameter is required.

Pow
Power.
Syntax: float pow (float base, float exp );
Return value: floating point number
Function type: mathematical operation
Description: This function calculates the power. Base is the base parameter, and exp is the power.

Example

<?phpprint(pow(2,3)); // 8print(pow(5,4)); // 625?> 

Reference: Log10 () Exp ()

Rand
Returns a random number.
Syntax: int rand ([int min], [int max]);
Return value: integer
Function type: mathematical operation
Description: This function is used to obtain random values. If the maximum and minimum ranges of random numbers are not specified, this function automatically retrieves a random number from 0 to RAND_MAX. If the min and max parameters are specified, a number is obtained from the specified parameters. For example, rand (38, 49) takes a random value from 38 to 49, UNIX systems include 49, Win32 systems do not include 49 ([email = zkimmel@earthlink.net] zkimmel@earthlink.net [/email] 10-May-1999 ). It is worth noting that in order to maximize the random number disorder, it is best to use srand () to configure the new random number seed before getting the random number.
Reference: srand () getrandmax () mt_rand () mt_srand () mt_getrandmax ()

Round
Rounding.
Syntax: double round (double val );
Return value: exact number of times
Function type: mathematical operation
Description: This function is used to round the number to the decimal point.

Example

<?php$foo1 = round(3.4);$foo2 = round(3.5);$foo3 = round(3.6);echo "round(3.4): ".$foo1."
n";echo "round(3.5): ".$foo2."
n";echo "round(3.6): ".$foo3;?>

Reference: Ceil () Floor ()

Sin
Sine calculation.
Syntax: float sin (float arg );
Return value: floating point number
Function type: mathematical operation
Description: This function calculates the sine value (sine) of the arg parameter ).
Reference: Cos () Tan ()

Sqrt
Square root.
Syntax: float sqrt (float arg );
Return value: floating point number
Function type: mathematical operation
Description: This function returns the square root of arg.

Srand
Configure the random seed.
Syntax: void srand (int seed );
Return value: None
Function type: mathematical operation
Description: This function configures the seed of the random number after passing in the seed parameter. It is worth noting that the seed value of the parameter is also a random number. for example, it is a good method to use the time added as the source of the variable, or to develop other hardware peripheral interfaces to obtain a better random number.

Example

In this example, we add the time factor to the execution of a random number of seeds every one second.

<?phpsrand((double)microtime()*1000000);$randval = rand();echo $randval;?> 

Reference: rand () getrandmax () mt_srand () mt_rand () mt_getrandmax ()

Tan
Returns the tangent.
Syntax: float tan (float arg );
Return value: floating point number
Function type: mathematical operation
Description: This function calculates the tangent of the arg parameter (tangent ).
Reference: Sin () Cos ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.