What are some alternative approaches to creating a variable-sized table in PHP without using nested loops?
When creating a variable-sized table in PHP without using nested loops, one alternative approach is to use a single loop to iterate over an array containing the desired number of rows. Within this loop, you can dynamically generate the table rows and cells based on the current iteration.
<?php
// Define the number of rows for the table
$num_rows = 5;
// Start the table
echo '<table>';
// Loop to generate rows
for ($i = 0; $i < $num_rows; $i++) {
echo '<tr>';
// Generate cells within each row
echo '<td>Row ' . ($i + 1) . ', Cell 1</td>';
echo '<td>Row ' . ($i + 1) . ', Cell 2</td>';
echo '</tr>';
}
// End the table
echo '</table>';
?>