How can PHP developers optimize code readability and maintainability when combining multiple SQL queries with UNION in a single function?

When combining multiple SQL queries with UNION in a single function, PHP developers can optimize code readability and maintainability by breaking down the queries into smaller, more manageable parts. This can be done by storing each query in a separate variable and then concatenating them together using UNION. Additionally, developers can use proper indentation, comments, and meaningful variable names to make the code more understandable.

// Separate SQL queries into individual variables
$query1 = "SELECT column1 FROM table1 WHERE condition1";
$query2 = "SELECT column2 FROM table2 WHERE condition2";

// Combine queries using UNION
$combinedQuery = $query1 . " UNION " . $query2;

// Execute the combined query
$result = mysqli_query($connection, $combinedQuery);

// Process the results
if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Do something with the data
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}