Convert From Feet to Inches Using PHP Script Code With HTML

Hello Friends Today, through this tutorial, I will tell you How can i Convert From Feet to Inches Using PHP Script Code With HTML.

Here’s the HTML and PHP code for a Feet to Inches converter:

feettoinches.php

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

Explanation:

HTML:

  1. The HTML defines the basic structure of the page with a heading, a form, and a paragraph to display the result.
  2. The form includes a label and an input field for entering the value in feet.
  3. The form action attribute points back to the same page (using `<?php echo htmlspecialchars($_SERVER[“PHP_SELF”]); ?>`) to handle the submission and display the result.

PHP:

  1. The script checks if the form is submitted using `isset($_POST[‘submit’])`.
  2. If submitted, it retrieves the value from the `feet` input field and converts it to a float.
  3. The conversion is performed by multiplying the feet by 12 (as there are 12 inches in a foot).
  4. Finally, it checks if `$converted_inches` is set and displays the result if it is.

This code provides a basic and functional way to convert feet to inches using HTML and PHP. You can extend this code by adding features like error handling and validation for the input value.