Hello Friends Today, through this tutorial, I will tell you How to Convert Meters to Feet Using PHP With HTML?.
Here’s the code for converting meters to feet using PHP with HTML.
mtofeet.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 Feet Converter</title> </head> <body> <h1>Meters to Feet 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 $conversion_rate = 3.28084; // Check if form is submitted if (isset($_POST['meters'])) { $meters = (float) $_POST['meters']; $converted_feet = $meters * $conversion_rate; } ?> <?php if (isset($_POST['meters'])): ?> <p><b><?php echo $_POST['meters']; ?> meters is equal to <?php echo $converted_feet; ?> feet.</b></p> <?php endif; ?> </body> </html>
Explanation:
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 meter equals 3.28084 feet).
5. Checks if the form is submitted using `isset($_POST[‘meters’])`.
6. Retrieves the entered value and converts it to feet using the conversion rate.
7. 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.