How can PHP be utilized to dynamically generate and display table columns based on user input?

To dynamically generate and display table columns based on user input in PHP, you can use a form to collect the user input (such as the number of columns they want to display), and then use a loop to generate the table columns dynamically based on that input.

<?php
if(isset($_POST['num_columns'])){
    $num_columns = $_POST['num_columns'];
    echo "<table>";
    echo "<tr>";
    for($i=1; $i<=$num_columns; $i++){
        echo "<th>Column $i</th>";
    }
    echo "</tr>";
    echo "</table>";
}
?>

<form method="post">
    <label for="num_columns">Enter number of columns:</label>
    <input type="text" name="num_columns">
    <input type="submit" value="Generate Table">
</form>