Convert from Kilometers to Inches PHP Script Code with HTML

Hello Friends Today, through this tutorial, I will tell you Convert from Kilometers to Inches php script code with html.

Here’s the PHP script with HTML code to convert from Kilometers to Inches:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kilometers to Inches Converter Using PHP With HTML Script Code</title>
</head>
<body>
<h1>Kilometers to Inches Converter Using PHP With HTML Script Code</h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label for="km">Enter value in Kilometers:</label>
<input type="number" name="km" id="km" required>
<br>
<button type="submit" name="submit">Convert</button>
</form>
</body>
</html>
<?php
// Conversion rate: 1 Kilometer = 39370.0787 Inches
$conversion_rate = 39370.0787;
// Check if form is submitted
if (isset($_POST['submit'])) {
$km = (float) $_POST['km'];
// Perform conversion
$inches = $km * $conversion_rate;
}
?>
<?php if (isset($km)): ?>
<p><b><?php echo $km; ?> kilometers is equal to <?php echo $inches; ?> inches.</b></p>
<?php endif; ?>

Explanation:

index.html:
This file creates a simple form with a label and an input field to enter the value in kilometers.
Defines the conversion rate from kilometers to inches.
Checks if the form is submitted.
Retrieves the value entered for kilometers.
Performs the conversion by multiplying the kilometers by the conversion rate.
Displays the converted value in inches on a same page.