Hello Friends Today, through this tutorial, I will tell you Convert from Kilometers to Miles php script code with html.
Here’s the PHP script with HTML code to convert kilometers to miles:
index.php:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Kilometers to Miles Converter</title> </head> <body> <h1>Kilometers to Miles Converter</h1> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> <label for="kilometers">Enter kilometers:</label> <input type="number" name="kilometers" id="kilometers" required> <br> <button type="submit" name="submit">Convert</button> </form> <?php if (isset($_POST['submit'])) { $kilometers = (float) $_POST['kilometers']; $miles = $kilometers * 0.621371; // Conversion factor: 1 km = 0.621371 miles echo "<p><b>$kilometers kilometers is equal to $miles miles.</b></p>"; } ?> </body> </html>
Explanation:
1. HTML: This part defines the basic structure of the web page, including the title, a heading, and a form.
2. Form: The form allows users to enter the value in kilometers and submit it for conversion.
3. PHP:
– `isset($_POST[‘submit’])`: This checks if the form has been submitted.
– `$kilometers = (float) $_POST[‘kilometers’];`: This retrieves the entered value and converts it to a float for calculations.
– `$miles = $kilometers * 0.621371;`: This performs the conversion using the conversion factor (1 km = 0.621371 miles).
– `echo`: This displays the converted value on the page.
Usage:
1. Save the code as `index.php` on your web server.
2. Open the file in your web browser (e.g., `http://localhost/index.php`).
3. Enter a value in kilometers and click the “Convert” button.
4. The page will display the equivalent value in miles.