How to Convert Meters to Centimeters Using PHP With HTML?

Hello Friends Today, through this tutorial, I will tell you How to Convert Meters to centimeters Using PHP With HTML. Here’s the code for converting meters to centimeters using PHP with HTML:

mtocm.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Meters to Centimeters Converter</title>
</head>
<body>
<h1>Meters to Centimeters 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>
<br>
<button type="submit">Convert</button>
</form>
<?php
// Conversion factor (1 meter = 100 centimeters)
$conversion_rate = 100;
// Check if form is submitted
if (isset($_POST['meters'])) {
$meters = (float) $_POST['meters'];
$converted_cm = $meters * $conversion_rate;
}
?>
<?php if (isset($_POST['meters'])): ?>
<p><b><?php echo $_POST['meters']; ?> meters is equal to <?php echo $converted_cm; ?> centimeters.</b></p>
<?php endif; ?>
</body>
</html>

Explanation-

1. Defines the basic structure with a heading and a form.
2. Uses a form to submit the entered value in 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 meter = 100 centimeters).
5. Checks if the form is submitted using `isset($_POST[‘meters’])`.
6. Retrieves the entered value and converts it to centimeters using the defined conversion rate.
7. Multiplies the entered meters by the conversion rate to get the equivalent value in centimeters.
8. Displays the converted value using string interpolation within the HTML code.

This code utilizes a simple form with PHP to perform the conversion and display the result within the HTML page.