Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>City Sorter</title> </head> <body> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get the cities from the form $cities = $_POST["cities"]; // Sort the cities alphabetically sort($cities); // Display the sorted cities echo "<h2>Sorted Cities</h2>"; echo "<ul>"; foreach ($cities as $city) { echo "<li>{$city}</li>"; } echo "</ul>"; } else { // Display the form echo "<h2>Enter 10 Cities</h2>"; echo "<form action='" . htmlspecialchars($_SERVER["PHP_SELF"]) . "' method='post'>"; for ($i = 1; $i <= 10; $i++) { echo "<label for='city{$i}'>City {$i}:</label>"; echo "<input type='text' name='cities[]' id='city{$i}' required><br>"; } echo "<br>"; echo "<input type='submit' value='Sort Cities'>"; echo "</form>"; } ?> </body> </html>
Copy Clear