PHP Math Functions | sin() | cos() | atan2()

Notes:

PHP Mathematical Constants & Functions:
- To perform mathematical calculations easily PHP provides various mathematical functions and constants.

- To find the position of a point on the Unit circle, we can take help of sine and cosine functions

sin(angleInRad:number):number
- Returns the y position of the point on the Unit circle
- Returns the value between -1 and +1

cos(angleInRad:number):number
- Returns the x position of the point on the Unit circle
- Returns the value between -1 and +1

- To find the angle of a point, we can take help of atan2 function

atan2(value1:number,value2:number):number
- Returns the angle of a point in radians
where value1 must be y position and value2 must be x position of a point.
Note: atan2 returns angle from -180 to 180

Example Code:
echo "x=",cos(deg2rad(0))," ","y=",sin(deg2rad(0)); // x=1 y=0
echo "x=",round(cos(deg2rad(90)))," ","y=",sin(deg2rad(90)); // x=0 y=1
echo "x=",cos(deg2rad(180))," ","y=",round(sin(deg2rad(180))); // x=-1 y=0

echo rad2deg(atan2(0,1)); // 0
echo rad2deg(atan2(1,0)); // 90
echo rad2deg(atan2(0,-1)); // 180
echo (rad2deg(atan2(-1,0)) + 360)%360; // 270

Interview Questions:

1. PHP stands for ______________
a. Hypertext Preprocessor
b. Preprocessor Hypertext
c. Personal Home Processor
d. Personal Hypertext processor
Ans: a