What are the advantages of using for-loops over while-loops in PHP for creating tables?
When creating tables in PHP, using for-loops can be advantageous over while-loops because for-loops are specifically designed for iterating a fixed number of times, making them ideal for tasks like creating rows in a table with a known number of iterations. Additionally, for-loops have a more concise syntax compared to while-loops, which can make the code easier to read and maintain.
<?php
// Using a for-loop to create a table with 5 rows
echo "<table>";
for ($i = 1; $i <= 5; $i++) {
echo "<tr><td>Row $i</td></tr>";
}
echo "</table>";
?>