How can PHP developers optimize the display of string content in a table format while limiting the length of certain fields?

To optimize the display of string content in a table format while limiting the length of certain fields, PHP developers can use the substr() function to truncate the string to a specified length before displaying it in the table. By limiting the length of certain fields, developers can ensure that the table layout remains consistent and visually appealing.

<?php
// Sample data
$data = [
    ['id' => 1, 'name' => 'John Doe', 'email' => 'johndoe@example.com'],
    ['id' => 2, 'name' => 'Jane Smith', 'email' => 'janesmith@example.com'],
];

echo '<table>';
echo '<tr><th>ID</th><th>Name</th><th>Email</th></tr>';
foreach ($data as $row) {
    echo '<tr>';
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . (strlen($row['email']) > 15 ? substr($row['email'], 0, 15) . '...' : $row['email']) . '</td>';
    echo '</tr>';
}
echo '</table>';
?>