PHP cos() Function

The cos() function returns the cosine ratio of given angle in radians. In trigonometry, cosine of an angle is defined as ratio of lengths of adjacent side and hypotenuse.

cos(x) = adjacent/hypotenuse

If x=90 degrees (?/2 radians), cos(x) = 0. This function returns a float value.

Syntax

cos ( float $arg ) : float

Parameters

Parameter Description
arg A floating point value that represents angle in radians

Return Value

PHP cos() function returns cosine ratio of given parameter as a float.

Examples

Basic Usage

Calculate cosine of ? (180 degrees) −

<?php
    $arg = M_PI;
    $val = cos($arg);
    echo "cos(" . $arg . ") = " . $val;
?>
cos(3.1415926535898) = -1

Common Angles

Calculate cosine for frequently used angles −

<?php
    // 0 degrees (0 radians)
    echo "cos(0) = " . cos(0) . "<br>";
    
    // 60 degrees (?/3 radians)
    echo "cos(?/3) = " . cos(M_PI/3) . "<br>";
    
    // 90 degrees (?/2 radians)
    echo "cos(?/2) = " . cos(M_PI/2) . "<br>";
    
    // 180 degrees (? radians)
    echo "cos(?) = " . cos(M_PI) . "<br>";
?>
cos(0) = 1
cos(?/3) = 0.5
cos(?/2) = 6.1232339957368E-17
cos(?) = -1

Converting Degrees to Radians

When working with degrees, convert them to radians first −

<?php
    function degToRad($degrees) {
        return $degrees * M_PI / 180;
    }
    
    $degrees = 45;
    $radians = degToRad($degrees);
    $cosValue = cos($radians);
    
    echo "cos({$degrees}°) = " . round($cosValue, 4);
?>
cos(45°) = 0.7071

Conclusion

The cos() function is essential for trigonometric calculations in PHP. Remember that it works with radians, so convert degrees using the formula: radians = degrees × ?/180.

Updated on: 2026-03-15T08:55:40+05:30

404 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements