How to Convert Meters to Kilometers Using PHP With HTML?

Hello Friends Today, through this tutorial, I will tell you How to Convert Meters to kilometers Using PHP With HTML.

Here’s the HTML and PHP code to convert meters to kilometers:


<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Meters to Kilometers Converter</title>
<h1>Meters to Kilometers Converter</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="meters">Enter value in meters:</label>
<input type="number" name="meters" id="meters" required>
<button type="submit">Convert</button>
// Define conversion factor
$conversion_rate = 1 / 1000; // 1 kilometer = 1000 meters
// Check if form is submitted
if (isset($_POST['meters'])) {
$meters = (float) $_POST['meters'];
$converted_km = $meters * $conversion_rate;
<?php if (isset($_POST['meters'])): ?>
<p><b><?php echo $_POST['meters']; ?> meters is equal to <?php echo $converted_km; ?> kilometers.</b></p>
<?php endif; ?>


1. Defines the basic structure with a heading, form, and result display area.
2. Uses a form to submit the value entered for meters.
3. Checks if `$_POST[‘meters’]` is set in the PHP code to display the result only after conversion.
4. Defines the conversion rate (1 kilometer is equal to 1000 meters).
5. Checks if the form is submitted using `isset($_POST[‘meters’])`.
6. Retrieves the entered value and converts it to kilometers.
7. Displays the converted value using string interpolation within the HTML code.