cos() expects an angle in radians and returns its cosine.
$angle = deg2rad(60);
echo cos($angle); // approximately 0.5
The examples below build from the basic call to practical variations and edge cases.
<?Php
echo COS(PI()); // Output is -1
echo "<br>";
echo COS(PI()/2); // Output 6.1232339957368E-17 // Same as 0
echo "<br>";
echo COS(PI()/3); // Output is 0.5
echo "<br>";
echo COS(PI()/4); // Output is 0.70710678118655
?>
Syntax
cos(float $num): float;
The argument is an angle in radians. Use deg2rad() to convert degree values before calling cos().
<?Php
echo cos(deg2rad(0)); // Output is 1
echo "<br>";
echo cos(deg2rad(180)); // Output is -1
?>
echo cos(100000); // Output: 0.9367521275331447
$angle = deg2rad(60); // Convert degrees to radians
$force = 100;
echo $force * cos($angle); // Output: 50 (Force in the x-direction)
$angle = 0;
echo cos(deg2rad($angle)); // Output: 1
$angle = 90;
echo cos(deg2rad($angle)); // Output: 6.1232339957368E-17 (close to 0)
$angle = -45;
echo cos(deg2rad($angle)); // Output: 0.70710678118655
These examples cover the cosine function for various angles, including 0, 90 degrees, and negative angles.
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.