PHP detects that the list of query variables is empty ()

I just want to optimize the code below. It works, but I want to see if anyone has a shorter way to write the following condition:

if(
!empty($_REQUEST['shipping_fname']) || 
!empty($_REQUEST['shipping_lname']) || 
!empty($_REQUEST['shipping_address1']) || 
!empty($_REQUEST['shipping_address2']) || 
!empty($_REQUEST['shipping_city']) || 
!empty($_REQUEST['shipping_state']) ||
!empty($_REQUEST['shipping_zip']) ||
!empty($_REQUEST['shipping_country'])){

        /* do stuff in here */

}

      

As you can see, there are some query variables that I am checking. If any of them are not empty, I will continue with the rest. (for those who need to know if any shipping information is transmitted, I'll do the check inside the brackets. Sometimes someone only sends the first, last and zip code.

Again this works, just wondering if anyone has a shorter methodology.

+3


source to share


1 answer


This would be the worst performance, and in fact I'm probably just sticking to my path, but a less burdensome alternative might be:

$arr = array('shipping_fname', 'shipping_lname', '...');
$go = false;
foreach ($arr as $f) { if (!empty($_REQUEST[$f])) { $go = true; break; } }
if ($go) { ... }

      



You can also do away with $ go if you like:

$arr = array('shipping_fname', 'shipping_lname', '...');
foreach ($arr as $f) {
    if (!empty($_REQUEST[$f])) {
        /* do stuff in here */
        break;
    } 
}

      

+2


source







All Articles