Use pi() or the M_PI constant when calculations require π. Both provide the same mathematical constant.
echo pi();
echo M_PI;
$circumference = 2 * pi() * 5;
echo $circumference;
This is the recommended starting pattern. The detailed examples below are retained and modernized for additional variations and explanation.
<?Php
echo PI(); // Output is 3.14159265358983
echo M_PI; // Output is 3.1415926535898
?>
Syntax
PI();
Contstant value equal to 3.1415926535898.
Math function Round is used to manage decimal places
<?Php
echo round(PI(),2); // Output is 3.14
echo "<br>";
echo round(PI()/2,2); // Output is 1.57
echo "<br>";
echo round(M_PI_4,2); // Output is 0.79
?>
echo M_PI_2; // Output is 1.5707963267949 // same as PI()/2
echo "<br>";
echo M_PI_4; // Output is 0.78539816339745 // same as PI()/4
echo M_1_PI; // Output is 1.5707963267949 // same as 1/PI()
echo "<br>";
echo M_2_PI; // Output is 0.78539816339745 // same as 2/PI()
Converting Rading to degree by rad2deg()
echo rad2deg(PI()); // Output is 180
echo "<br>";
echo rad2deg(PI()/2); // Output is 90
echo "<br>";
echo rad2deg(PI()/3); // Output is 60
echo "<br>";
echo rad2deg(PI()/4); // Output is 45
<?Php
echo round(sin(pi()),2);// Output is 0
echo "<br>";
echo sin(pi()/2); // Output is 1
echo "<br>";
echo cos(pi()); // Output is -1
echo "<br>";
echo round(cos(PI()/2),2); // Output is 0
?>
We used Round function to manage decimal places. Plotting of SIN & COS curves by using PHP GD function with deg2rad()
Math Functions SIN() function rad2deg() functionAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.