PHP Tutorial - PHP cos() Function






Definition

cos() calculates the cosine value.

Syntax

PHP cos() Function has the following format.

float cos ( float num )

Parameter

Parameter Is Required Description
number Required.A number

The parameter should be passed as radians. We should use deg2rad() to convert degrees to radians.

Return

Item Description
Return ValueThe cosine of number
Return TypeFloat

The cos() function returns a numeric value between -1 and 1, which represents the cosine of the angle.





Example

Return the cosine of different numbers:


<?php/*  ww w  . j a  v a 2 s  . c  om*/
    $cos1 = cos(10); 
    print($cos1);
    echo("\n");
    $cos2 = cos(deg2rad(80)); 
    print($cos1);
    echo("\n");
    echo(cos(3) . "\n");
    echo(cos(-3) . "\n");
    echo(cos(0) . "\n");
    echo(cos(M_PI) . "\n");
    echo(cos(2*M_PI));
?>

The code above generates the following result.