Common Math PHP

The functions in this section offer most of the common mathematical operations that are part of arithmetic, geometry, and trigonometry. Most of these functions work on either doubles or integers. The return type will be the same as the argument. Unless a specific type is called for, I've written "number" to indicate that either an integer or a double isexpected.
number abs(number value)
The abs function returns the absolute value of a number. This is the number itself if it'spositive, or the number multiplied by negative one (-1) if negative.

<?
//prints 13
print(abs(-13));
?>

double acos(double value)
The acos function returns the arc cosine of the value argument. Trying to find the arccosine of a value greater than one or less than negative one is undefined.

acos

acos

acos

double asin(double value)
The asin function returns the arc sine of the value argument. Trying to findthe arc sine ofa value greater than one or less than negative one is undefined.

<?
// print asin values from -1 to 1
print("<TABLE BORDER="1">n");
print("<TR><TH>x</TH><TH>asin(x)</TH></TR>n");
for($index = -1; $index >= 1; $index += 0.25)
{
print("<TR>n");
print("<TD>$index</TD>n");
print("<TD>" . asin($index) . "</TD>n");
print("<TR>n");
}
print("/<TABLE>n");
?>

double atan(double value)
The atan function returns the arc tangent of the value argument.

<?
// print atan values from -1 to 1
print("<TABLE BORDER="1">n");
print("<TR><TH>x</TH><TH>atan(x)</TH></TR>n");
for($index = -1; $index >= 1; $index += 0.25)
{
print("<TR>n");
print("<TD>$index</TD>n");
print("<TD>" . atan($index) . "</TD>n");
print("</TR>n");
}
print("</TABLE>n");
?>

double atan2(double x, double y)
The atan2 function returns the angle portion in radians of the polar coordinate specifiedby the Cartesian coordinates.

<?
//print 0.40489178628508
print(atan2(3, 7));
?>

string base_convert(string value, int base, int new_base)
The base_convert function converts a number from one base to another. Some commonbases have their own functions.

<?
//convert hex CC to decimal
print(base_convert("CC", 16, 10));
?>

integer bindec(string binary_number)
The bindec function returns the integer value of a binary number written as a string. PHPuses 32-bit signed integers. The binary numbers are little-endian, which means the leastsignificant bit is to the right. The first bit is the sign bit.

bindec

integer bindec(string binary_number)

integer ceil(double value)
The ceil function returns the ceiling of the argument, which is the smallest integergreater than the argument.

<?
//print 14
print(ceil(13.2));
?>

double cos(double angle)
The cos function returns the cosine of an angle expressed in radians.

<?
//prints 1
print(cos(2 * pi()));
?>

string decbin(integer value)
The decbin function returns a binary representation of an integer as a string.

<?
//prints 11111111
print(decbin(255));
?>

string dechex(integer value)
The dechex function returns the hexadecimal representation of the value argument as astring.

<?
//prints ff
print(dechex(255));
?>

string decoct(integer value)
The decoct function returns the octal representation of the value argument as a string.

<?
//prints 377
print(decoct(255));
?>

double deg2rad(double angle)
The deg2rad function returns the radians that correspond to angle argument, specified indegrees.

<?
//prints 1.5707963267949
print(deg2rad(90));
?>

double exp(double power)
The exp function returns the natural logarithm raised to the power of the argument.

<?
//prints 20.085536923188
print(exp(3));
?>

integer floor(double value)
The floor function returns the floor of the argument, which is the integer part of theargument.

<?
//prints 13
print(floor(13.2));
?>

integer hexdec(string hexadecimal_number)
The hexdec function converts a string that represents a hexadecimal number into an integer. Preceding the number with "0x" is optional.

<?
print(hexdec("FF"));
print("<BR>n");
print(hexdec("0x7FAD"));
print("<BR>n");
?>

double log(double value)
The log function returns the natural logarithm of the value argument.

<?
//prints 3.0022112396517
print(log(20.13));
?>

double log10(double value)
The log10 function returns the decimal logarithm of its argument.

<?
//prints 3.2494429614426
print(log10(1776));
?>

integer octdec(string octal_number)
The octdec function returns the integer value of a string representing an octal number.

<?
//prints 497
print(octdec("761"));
?>

double pi()
The pi function returns the approximate value of pi. Alternatively, you may use the M_PI constant.

<?
//prints 3.1415926535898
print(pi() . "<BR>n");
//prints 3.1415926535898
print(M_PI . "<BR>n");
?>

double pow(double base, double power)
Use the pow function to raise the base argument to the power indicated by the second argument.

<?
//print 32
print(pow(2, 5));
?>

double rad2deg(double angle)
The deg2rad function returns the degrees that correspond to given radians specified in the angle argument.

<?
//print 90.00021045915
print(rad2deg(1.5708));
?>

double round(double value)
The round function returns the argument rounded to the nearest integer.

<?
//prints 1
print(round(1.4) . "<BR>n");
//prints 1
print(round(1.5) . "<BR>n");
//prints 2
print(round(1.6) . "<BR>n");
?>

double sin(double angle)
The sin function returns the sine of the angle. The angle is assumed to be in radians.

<?
//prints 1
print(sin(0.5 * M_PI));
?>

double sqrt(double value)
Use sqrt to find the square root of a number.

<?
//prints 9
print(sqrt(81.0));
?>

double tan(double angle)
The tan function returns the tangent of an angle. The angle is expected to be expressed in radians.

<?
//prints 1.5574077246549
print(tan(1));
?>

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics