Concept

How can PHP libraries and math functions be used?

ComputerScienceOne / Comments

"PHP has many built-in functions that you can use. These standard libraries are loaded and available to use without any special command to import or include them. Full documentation on each of these functions is maintained in the PHP manual, available online at http://php.net/. The manual’s web pages also contain many curated comments from PHP developers which contain further explanations, tips & tricks, suggestions, and sample code to provide further assistance. There are many useful functions that we’ll mention as we progress through each topic. One especially useful collection of functions is the math library. This library is directly adapted from C’s standard math library so all the function names are the same. It provides many common mathematical functions such as the square root and natural logarithm. Table 37.1 highlights several of these functions; full documentation can be found in the PHP manual (http://php.net/manual/en/ref.math.php). To use these, you simply “call” them by providing input and assigning the output to a variable.\n\n$x = 1.5;\n\n$y = sqrt($x); // y now has the value √x = √1.5\n\n$z = sin($x); // z now has the value sin(x) = sin(1.5)\n\nIn both of the function calls above, the value of the variable $x is “passed” to the math function which computes and “returns” the result which then gets assigned to another variable.\n\nTable 37.1: Several functions defined in the PHP math library.\n\nabs($x): Absolute value, |x|\n\nceil($x): Ceiling function\n\nfloor($x): Floor function\n\ncos($x): Cosine function\n\nsin($x): Sine function\n\ntan($x): Tangent function\n\nexp($x): Exponential function, e^x, e = 2.71828...\n\nlog($x): Natural logarithm, ln(x)\n\nlog10($x): Logarithm base 10\n\npow($x,$y): The power function, computes x^y\n\nsqrt($x): Square root function\n\nAll trigonometric functions assume input is in radians, not degrees. Input for the logarithm functions is assumed to be positive, x > 0. Alternatively, PHP supports exponentiation by using x ** y."

Related Ideas

How can PHP libraries and math functions be used? | ComputerScienceOne | Bifalgorithm | Bifalgorithm