What are the potential pitfalls of using echo to output HTML tables in PHP?
One potential pitfall of using echo to output HTML tables in PHP is that it can make the code harder to read and maintain, especially for larger tables. To solve this issue, you can use a combination of PHP's control structures and concatenation to generate the table markup in a more organized manner.
<?php
// Sample data for the table
$data = [
['John Doe', 'john.doe@example.com'],
['Jane Smith', 'jane.smith@example.com'],
['Mike Johnson', 'mike.johnson@example.com']
];
// Start building the table markup
$table = '<table>';
$table .= '<tr><th>Name</th><th>Email</th></tr>';
// Loop through the data to populate the table rows
foreach ($data as $row) {
$table .= '<tr>';
foreach ($row as $cell) {
$table .= '<td>' . $cell . '</td>';
}
$table .= '</tr>';
}
$table .= '</table>';
echo $table;
?>