Create a file named `percentage_calculator.php` with the following content:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Percentage Calculator</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } label { display: block; margin-bottom: 8px; } input[type="number"] { width: 100%; padding: 8px; margin-bottom: 16px; box-sizing: border-box; } button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #45a049; } </style> </head> <body> <h2>Percentage Calculator</h2> <form method="post"> <label for="number">Enter Number:</label> <input type="number" id="number" name="number" placeholder="Enter a number" required><br> <label for="percentage">Enter Percentage:</label> <input type="number" id="percentage" name="percentage" placeholder="Enter a percentage" required><br> <button type="submit">Calculate</button> </form> <?php // Check if form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Retrieve form data $number = $_POST["number"]; $percentage = $_POST["percentage"]; // Calculate percentage $result = ($number * $percentage) / 100; // Display result echo "<h3>Result:</h3>"; echo "<p>{$percentage}% of {$number} is {$result}</p>"; } ?> </body> </html>
This PHP script displays a simple HTML form where users can input a number and a percentage. When the form is submitted, the PHP script calculates the percentage and displays the result below the form.
You can run this PHP script on a server with PHP support. Place the file in your server’s document root directory and access it through a web browser. For example, if your server is running locally, you can access it at `http://localhost/percentage_calculator.php`.