What are the benefits of directly outputting data within the while loop instead of storing it in arrays?

Outputting data directly within the while loop instead of storing it in arrays can be beneficial in terms of memory usage and performance. By outputting data immediately, you can avoid storing large amounts of data in memory, which can be especially useful when dealing with large datasets. Additionally, it can improve the overall efficiency of your code as you are not required to loop through the data again to output it.

// Connect to database
$connection = new mysqli('localhost', 'username', 'password', 'database');

// Query data from database
$result = $connection->query("SELECT * FROM table");

// Output data directly within the while loop
while ($row = $result->fetch_assoc()) {
    echo $row['column1'] . ' - ' . $row['column2'] . '<br>';
}

// Close database connection
$connection->close();