Simple PHP script for converting Euro to USD using a fixed exchange rate. Enter Euro amount and get the converted value with validation and error handling.
<?php
// Definition of the fixed exchange rate Euro -> Dollar
const TASSO_CAMBIO = 1.1; // 1 Euro = 1.1 USD
// Variables to store input and result
$importoEuro = '';
$importoUSD = null;
$errore = '';
// Check if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// Clean and validate the input
if (isset($_POST['importoEuro'])) {
$importoEuro = trim($_POST['importoEuro']);
// Check that it is a positive number
if (is_numeric($importoEuro) && $importoEuro >= 0) {
// Conversion
$importoUSD = $importoEuro * TASSO_CAMBIO;
} else {
$errore = "Please enter a valid amount (positive number).";
}
} else {
$errore = "No amount entered.";
}
}
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<title>Euro -> Dollar Converter</title>
<style>
body { font-family: Arial, sans-serif; margin: 2em; }
label, input, button { font-size: 1em; }
.errore { color: red; }
.risultato { margin-top: 1em; font-weight: bold; }
</style>
</head>
<body>
<h1>Mini currency converter</h1>
<form method="post" action="">
<label for="importoEuro">Amount in Euro:</label>
<input type="text" id="importoEuro" name="importoEuro" value="<?php echo htmlspecialchars($importoEuro); ?>" required>
<button type="submit">Convert</button>
</form>
<?php if ($errore): ?>
<p class="errore"><?php echo htmlspecialchars($errore); ?></p>
<?php elseif ($importoUSD !== null): ?>
<p class="risultato"><?php echo number_format($importoEuro, 2, ',', '.'); ?> € correspond to <?php echo number_format($importoUSD, 2, ',', '.'); ?> $</p>
<?php endif; ?>
</body>
</html>PHP 7.0+ installed and configured
Local or remote web server (e.g. Apache, Nginx)
PHP file for user interface and conversion logic
CONFIG: fixed exchange rate defined inside the code (e.g. 1 Euro = 1.1 USD)
DEPENDENCIES: no external libraries required