How can PHP be used to create a dynamic list on a website that can be updated by user input?

To create a dynamic list on a website that can be updated by user input, you can use PHP to store the list items in an array or database. Users can input new items through a form, which will then be processed by PHP to update the list. The updated list can be displayed on the website dynamically.

<?php
$listItems = []; // Initialize an empty array to store list items

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get user input from form
    $newItem = $_POST["newItem"];
    
    // Add new item to the list array
    $listItems[] = $newItem;
}

// Display the list items
echo "<ul>";
foreach ($listItems as $item) {
    echo "<li>$item</li>";
}
echo "</ul>";
?>

<!-- HTML form for user input -->
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    <input type="text" name="newItem" placeholder="Enter new item">
    <button type="submit">Add Item</button>
</form>