Jump to content

if() with two conditions OR nested if() what is best


anatak

Recommended Posts

What do you think is the best way ?

 

I am testing two arrays to see if the corresponding values are empty or not

 

for($t=0;$t<count($array1);$T++){
if(empty($array1[$t]) AND empty($array2[$t])){}
if(empty($array1[$t]) AND !empty($array2[$t])){}
if(!empty($array1[$t]) AND empty($array2[$t])){}
if(!empty($array1[$t]) AND !empty($array2[$t])){}
}

 

OR

for($t=0;$t<count($array1);$T++){
  if(empty($array1[$t]){
    if(empty($array2[$t]){
    //empty($array1[$t]) AND empty($array2[$t]
    }else{
    //empty($array1[$t]) AND !empty($array2[$t]
    }
  }else{
  if(!empty($array2[$t])){
    //!empty($array1[$t]) AND !empty($array2[$t]
    }
  }

 

what would be the fastest code ?

the first one is a lot more readable I think than the second one but I would guess that the second one would be more faster.

could you also explain why you think which code would be better ?

 

 

thank you

anatak

neither best is using if statements properly!

 

for($t=0;$t<count($array1);$T++){
  if(empty($array1[$t]) AND empty($array2[$t])){
  } elseif(empty($array1[$t]) AND !empty($array2[$t])){
  } elseif(!empty($array1[$t]) AND empty($array2[$t])){
  } elseif(!empty($array1[$t]) AND !empty($array2[$t])){
  }
}

 

^ thats the fastest because it may only check the first if then exit.. to next loop

or it may check 2 if's and exit..

but if you add a bunch of if's it will check them all!

 

Unless you want them to all be checked?

neither best is using if statements properly!

 

for($t=0;$t<count($array1);$T++){
  if(empty($array1[$t]) AND empty($array2[$t])){
  } elseif(empty($array1[$t]) AND !empty($array2[$t])){
  } elseif(!empty($array1[$t]) AND empty($array2[$t])){
  } elseif(!empty($array1[$t]) AND !empty($array2[$t])){
  }
}

 

^ thats the fastest because it may only check the first if then exit.. to next loop

or it may check 2 if's and exit..

but if you add a bunch of if's it will check them all!

 

Unless you want them to all be checked?

 

 

I am such an idiot

why didn't I think about elseif()

thanks

I also thinks this is the most readable solution

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.