Using a certain query field based on state I need to be able to do a select to a table using a where clause that can use an email or phone field, depending on if they are set/not empty or not. Given this context, how can I simplify this PHP code?

 if(isset($email) &amp;&amp; !empty($email) &amp;&amp; isset($phone) &amp;&amp; !empty($phone)){
        $partQuery = 'account_id = '.$account-&gt;id.' AND (phone = "'.(int)$phone.'" OR email = "'.$email.'")';
    }

    if(isset($phone) &amp;&amp; !empty($phone)){
        $partQuery = 'account_id = '.$account-&gt;id.' AND phone = "'.(int)$phone.'"';
    }

    if(isset($email) &amp;&amp; !empty($email)){
        $partQuery = 'account_id = '.$account-&gt;id.' AND email = "'. $email.'"';
    }
First of all, use prepared statements.

The account id part is always the same, so extract it to the beginning.

Also, you can extract the isset and not empty part to a function to simplify the code.

$partQuery = 'account_id = '.$account-&gt;id;

if(isNotEmpty($phone) &amp;&amp; isNotEmpty($email)) {
    $partQuery .= ' AND (phone = "'.(int)$phone.'" OR email = "'.$email.'")';
}
if(isNotEmpty($phone)) {
    $partQuery .= ' AND phone = "'.(int)$phone.'"';
}
if(isNotEmpty($email)) {
    $partQuery .= ' AND email = "'. $email.'"';
}

function isNotEmpty($var) {
    return isset($var) &amp;&amp; !empty($var);
}


Although I would assume that that is not actually what you want, as it adds two additional ANDs in case neither phone nor email is empty.

It also seems like an odd query in general. You are fine with a wrong phone number as long as the email is correct and vise versa?

Also note that you don't actually need isset, you can just use !empty, it will not generate a warning.