Mini fixed-rate currency converter in PHP

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.

Brief Introduction

Mini fixed-rate currency converter in PHP (php)
DESCRIPTION:
A simple PHP script that allows converting an amount from Euro to US Dollars using a fixed exchange rate. The user enters the amount in Euro and the system returns the converted value in Dollars.

ADDITIONAL NOTES:
The exchange rate is fixed and defined in the code, so it does not update automatically. It is a basic example to understand form handling and calculations in PHP.

EXPLANATIONS:
- The HTML form allows the user to enter an amount in Euro.
- The PHP code checks that the input is valid (positive number).
- The exchange rate is defined as a constant for ease of modification.
- The conversion is done by multiplying the amount by the exchange rate.
- The result is shown to the user with two decimals.
- If the input is invalid, an error message is displayed.

Example Code: php

<?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, ',', '.'); ?> &euro; correspond to <?php echo number_format($importoUSD, 2, ',', '.'); ?> $</p>
    <?php endif; ?>
</body>
</html>
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

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

Similar Topics

First PHP script: display a message on the screen
Learn how to create your first PHP script that displays a welcome message on the screen. Perfect for beginners to see immediate results with PHP code.
PHP page displaying the current date and time
Simple PHP script displaying current date and time in day/month/year hour:minute:second format with timezone settings for accurate local time display.
Very Simple Calculator with Two Numbers in PHP
A very simple PHP calculator script that adds two user-entered numbers via an HTML form and displays the result on the same page securely and efficiently.
PHP Form that Greets the User by Name
Simple PHP form that greets users by name using POST method with secure input handling and beginner-friendly code in a single PHP file.
PHP page that counts how many times it has been visited
Simple PHP script that counts and displays the number of visits to a web page using a writable text file to store the counter value securely.
Static product list displayed with PHP
Simple PHP example displaying a static product list with name description and price using arrays and foreach loop for beginners learning PHP.
PHP page that generates an HTML table from an array
Learn how to create a simple PHP page that dynamically generates an HTML table from a multidimensional array with secure and easy to understand code.
Mini address book in PHP with a contacts array
A simple PHP mini address book script using an array to display and search contacts by name with case insensitive search and no database needed.