How can the use of JOIN statements in SQL queries improve the efficiency and readability of PHP code?

Using JOIN statements in SQL queries can improve the efficiency and readability of PHP code by allowing for more complex data retrieval operations to be performed directly in the database, rather than manipulating multiple result sets in PHP. This can reduce the amount of data transferred between the database and the PHP script, leading to faster query execution times. Additionally, JOIN statements can simplify the logic of the PHP code by handling data relationships at the database level, making the code easier to understand and maintain.

<?php
// Example SQL query with JOIN statement
$sql = "SELECT users.username, orders.order_id
        FROM users
        INNER JOIN orders ON users.user_id = orders.user_id
        WHERE users.status = 'active'";

// Execute the query and fetch results
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) {
    echo "Username: " . $row['username'] . " | Order ID: " . $row['order_id'] . "<br>";
}

// Close the database connection
mysqli_close($connection);
?>