How to Create Cosine Calculator using PHP with Example?

Hello Friends Today, through this tutorial, I will tell you How to Write Program Cosine calculator using PHP with HTML. Sure! Here’s a simple example of a cosine calculator using PHP with HTML:

index.php

<!DOCTYPE html>
<html>
<head>
<title>Cosine Calculator</title>
</head>
<body>
<h2>Cosine Calculator</h2>
<form method="post" action="">
Enter an angle in degrees: <input type="text" name="angle" required><br><br>
<input type="submit" name="submit" value="Calculate">
</form>

<?php
// Function to calculate cosine
function cosine($angle) {
return cos(deg2rad($angle));
}

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Retrieve angle from form input
$angle = $_POST["angle"];

// Validate input
if (!is_numeric($angle)) {
echo "Please enter a valid number.";
} else {

// Calculate cosine
$result = cosine($angle);
echo "Cosine of $angle degrees is: $result";
}
}
?>
</body>
</html>

This code creates a simple HTML form where users can input an angle in degrees. When the form is submitted, the PHP code calculates the cosine of the entered angle and displays the result below the form. The `cosine()` function takes an angle in degrees as input and returns its cosine. The `deg2rad()` function is used to convert degrees to radians, as the `cos()` function in PHP expects angles to be in radians.