What role do logical operators like AND and OR play in constructing SQL queries in PHP?

Logical operators like AND and OR play a crucial role in constructing SQL queries in PHP as they allow us to combine multiple conditions to filter data effectively. By using these operators, we can retrieve only the records that meet certain criteria specified in the query. This helps us to retrieve the exact data we need from the database.

// Example SQL query using logical operators AND and OR
$query = "SELECT * FROM users WHERE age > 18 AND (city = 'New York' OR city = 'Los Angeles')";
$result = mysqli_query($connection, $query);

// Process the result set
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "Name: " . $row['name'] . ", Age: " . $row['age'] . ", City: " . $row['city'] . "<br>";
    }
} else {
    echo "No results found.";
}