How can the modulo operator be used to efficiently display data in rows of 2 entries in PHP?
To efficiently display data in rows of 2 entries in PHP, we can use the modulo operator (%) to determine when to start a new row. By checking if the current index is divisible by 2, we can insert a line break to create the desired 2-entry rows.
$data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach ($data as $index => $entry) {
echo $entry;
if (($index + 1) % 2 == 0) {
echo "<br>";
} else {
echo " ";
}
}