0

Before looping through a variable (supposed to be an array) I would make the following test:

if(
        !empty($arrJobs)    &&
        is_array($arrJobs)  &&
        count($arrJobs)
){
foreach ($arrJobs as $item) {
  //loop tasks
}
}

I've seen also using if(sizeof($arrJobs)>0) but in case $arrJobs isa integer (I can't trust the input) it would be through and I will try looping through a integer...weird.

Is there a more concise and comprehensive way to perform this test before looping through the array?

koalaok
  • 5,075
  • 11
  • 47
  • 91
  • Possible duplicate of [How to check if variable is array?... or something array-like](http://stackoverflow.com/questions/15603952/how-to-check-if-variable-is-array-or-something-array-like) – Naruto Sep 23 '16 at 08:56

2 Answers2

1

It is IMPORTANT to check if you're looping an ARRAY to FOREACH or else PHP will give you errors ... FOREACH expects parameter to be ARRAY..

You can use IS_ARRAY .. I believe that would be enough.

But checking it if empty would be VERY GOOD Practice... I'm using COUNT() to check if an ARRAY is empty.

Emz
  • 502
  • 1
  • 3
  • 11
0

You don't need to check anything but is_array: if it's empty, PHP simply won't loop.

Moreover a suggestion: if those if had sense (not that case, but is helpful for this explanation) you should split with "early exit" (as coding style because it improves readability)

if (empty($arrJobs)) {
 return;
}

if (!is_array($arrJobs)) {
 return;
}

if (count($arrJobs)) {
 return;
}
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • do they have sense if you need to do something (or report something ) if array is empty? don't they? – koalaok Sep 23 '16 at 08:59
  • 1
    @DonCallisto PHP has short-circuit condition evaluation, so after evaluation of first condition as false it skips other comparsions, so this "early-exit" is useless, just more code. – Jan Holas Sep 23 '16 at 09:01
  • is_array seems to have some sense... if the variable has another type: Invalid argument supplied for foreach() – koalaok Sep 23 '16 at 09:01
  • @JanHolas yes I *perfectly* know this ... Mine suggestion was about readability – DonCallisto Sep 23 '16 at 09:02
  • on the other hand... even if PHP let you play dirty, it doesn't mean you shouldn't worry about writing code in a solid flavour.(if it doesn't affect performance, and not adding garbage lines) IMHO – koalaok Sep 23 '16 at 09:03
  • 1
    This "early exit suggestion" certainly does _not_ raise readability! – arkascha Sep 23 '16 at 09:04
  • @DonCallisto I don't think it's more readble, but it's probably just matter of style and opinion. It might be usefull if you want to display different error message for each condition. – Jan Holas Sep 23 '16 at 09:04
  • @koalaok no one let play you dirty: if something is array, so it's traversable, and it's empty, you should not check it *on purpose*. fullstop. – DonCallisto Sep 23 '16 at 09:04
  • @KOALAOK.. check my answer below – Emz Sep 23 '16 at 09:04
  • @arkascha what if you have, let's say, ten `&&` condition? – DonCallisto Sep 23 '16 at 09:06
  • @victor yep I agree with you. Upvoted and going to accept it. It would be great if php had a function that would check both is_array && !empty at the same time. – koalaok Sep 23 '16 at 09:06
  • @Koalaok ... well you can create a function yourself... that would return a boolean... so you can just call that function in your if-statement .. and you can use it to your other projects.. .. make sense? :-) – Emz Sep 23 '16 at 09:08
  • @arkascha I think the right way for readability stays in the middle. – koalaok Sep 23 '16 at 09:10
  • @victor makes sense. But it's annoying to have to complete a lang with utility function that could have been embedded :-) So for my question sake the answer is test empty+is_array – koalaok Sep 23 '16 at 09:13
  • @DonCallisto The _number_ of boolean conditions you concatenate does not lower readability. In contrary, blowing those checks up over a full page does. It is more a question of indentation. It is absolutely obvious to a programmer how those boolean operators behave. I personally place the `&&` (or whatever operator) at the _beginning_ of each line. That makes things crystal clear. – arkascha Sep 23 '16 at 09:14
  • @Koalaok ... You Got It.. go kick some App!.. ;-) – Emz Sep 23 '16 at 09:19
  • @Koalaok ... before I forgot.. you don't have to separate the condition.. you can use Logical && ... ;-) – Emz Sep 23 '16 at 09:37