What Will I Learn?
- PHP Math Functions
Requirements
- PHP
- Linux
Difficulty
- Basic
Trigonometry
PHP, has provided ready-made functions to perform trigonometric calculations and the following table shows which functions these functions perform.
Function | Explanation |
---|---|
sin() | It generates the sine value of the input radian expression. |
cos() | It generates the cosine value of the entered radian expression. |
tan() | It generates the tangent value of the input radian expression. |
asin() | It produces the inverse sinusoidal value of the input expression. |
acos() | Produces the inverse cosine value of the input expression. |
atan() | Generates the inverse tangent value of the input expression. |
<?php
$value = 90;
echo sin ($value) . '
'; #Result: 0.893996663601
echo sin (deg2rad($value) ) . '
'; #Result: 1
echo sin (pi()/2) . '
'; #Result: 1
?>
Above is an example of the sin() function. Similarly, the cos() and tan() functions are used in this way. When we examine the object, we can see that 0.89 float value is returned when 90 (integer) is sent to sin() function.
<?php
$value = 1;
$radians = asin ($value);
echo $radians. '
' ; #Result: 1.57079632679
echo rad2deg($radyan) . '
' ; #Result: 90
?>
In the above example, I gave an example through the asin() function. You can also use the acos() and assign() functions in the same way. In the example you want to do a reverse sine calculation for y=1. Therefore, when the value of the inverse sinus is calculated, the result will be in terms of radians. We can get the results by printing the calculated radian result and the converted degree of the radian on the screen.
Random Number Poducers
For example;
Random number generators are used in password generators or in part of the algorithm in applications that perform different operations according to the value of a randomly generated number. In this section, we will examine the random number functions that you can use with PHP. The following table contains the functions and explanations required to create a random number.
Function | Explanation |
---|---|
rand() | Produces a random number. |
getrandmax() | It shows the largest possible value that can be generated with the rand() function. |
mt_rand() | It generates random numbers in faster time. |
mt_getrandmax() | It shows the largest possible value that can be generated with the mt_getrandmax() function. |
<?php
echo 'rand: ' . getrandmax () . '
' ; #Result: 32767
echo rand () . '
';
echo rand (0,10) . '
';
echo 'mt_rand: ' .mt_getrandmax() . '
'; #Result: 2147483647
echo mt_rand () . '
' ;
echo mt_rand (20, 30) ;
?>
When you run the above example, different numbers will be generated each time. Only the values of getrandmax() and mt_getrandmax() will be fixed. These two functions were used for informational purposes. If you use rand() without any parameters, it returns a random number between 0 and getrandmax(). If min and max are specified, a random number is generated between these two values. For example; In the case of mt_rand (20,30) it means that a random number will be generated from the numbers 20 to 30 using the mt_rand() function.
Other Functions
There are a few more functions I could not fit into the above headers. We will come to the end of functions that perform mathematical calculations by specifying these functions.
Function | Explanation |
---|---|
max() | Returns the largest of the numbers sent as a parameter. |
min() | Returns the smallest of the numbers sent as a parameter. |
pi() | Returns the absolute PI number, deg2rad (180). |
<?php
echo max (2,80,1.5, -20, 70) . '
' ;
echo min (2,80,1.5, -20, 70). '
';
# Various ways of obtaining PI
echo pi () . '
' ;
echo deg2rad(180) . '
' ;
echo M_Pİ . '
' ;
?>
Our mathematical functions are finished here. Thank you for taking the time and reading it.
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors