to use an "if empty" statement, or not

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

to use an "if empty" statement, or not

Post by infolock »

A friend and I have been debating on an issue and it's really hard to come up with a valid argument for or against it.

The situation:

===================================================================================================================

(Without any checks)

Code: Select all

 
<?php
$array = array();
$array = dynamic_function();  // This could return 1200 array values, no values, 1 value, etc.
$x = count($array);
for($i = 0; $i < $x; $i++) {
}
?>
 
===================================================================================================================
(With Checks)

Code: Select all

 
<?php
$array = array();
$array = dynamic_function();  // This could return 1200 array values, no values, 1 value, etc.
if(!empty($array)) {
  $x = count($array);
  for($i = 0; $i < $x; $i++) {
  }
}
?>
 
===================================================================================================================

The debate:

We have 2 checks now. One just let's the for loop determine if it needs to execute. The second determines if there is truly a reason for the for loop to execute, it will execute only then.

The argument is, only when the value is truely empty, will the if statement make sense. When it's not empty, the empty function will add more time to the overall performance of the application due to having to check if is empty.

Here is also another bit of information: if you run the empty function on a blank array, it evaulates a LOT quicker than if you evaluate on an array with 1200+ records.

So, now the question is this: which way would you use, and why?

I personally prefer checking to see if the array has value, and only then executing a for loop. This is because there is a count, and then a for loop function call, and checking if it has value before executing it prevents the uncessary run.

The counter argument is if the array does have value, that it will run even slower due to the initial if statement.

The counter to the counter is that, in my opinion, you should be checking to see if that block of code is valid to be run at all, meaning if you do take the .0002 seconds to evaluate it's in all reality perfectly valid due to not knowing one way or another if the operations are absolutely needed or not.

What say you?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: to use an "if empty" statement, or not

Post by pickle »

Assuming the time to execute empty() and count() on an empty array is negligible, I'd say get rid of the empty(). You're not saving any time by having it there for an empty array, and you're only adding a non-negligible time to execution when the array isn't empty.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: to use an "if empty" statement, or not

Post by infolock »

pickle wrote:Assuming the time to execute empty() and count() on an empty array is negligible, I'd say get rid of the empty(). You're not saving any time by having it there for an empty array, and you're only adding a non-negligible time to execution when the array isn't empty.
Actually, the empty function, on an empty array, does allow the application to run faster versus just letting the for loop run on an empty array. In other words, if we take the for loop out, and it executes on an empty array set, then it takes longer for it to calculate and run.

The argument is that this only applies when the array is empty. If the array has 1200 records, then this is when it has a negative impact on the code execution.

It's kind of a debatble thing here. I'm not really sure there is a legitament claim on either side. I personally feel that if I'm dealing with a dynamic array/element/variable that I'm not sure is going to be empty or full, and then I want to do something with said item, then I should be doing a check to see if the code should continue and execute rather than execute and waste all this time running stuff that will never ring true when the said item is blank (major run on sentence, too tired to care).

Anyways, that's the point I guess. Any other thoughts on this?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: to use an "if empty" statement, or not

Post by Christopher »

The for() in conjunction with the count() is in essence an if() check, so the redundant check is just more code. For me the error free code path should be the shortest. The code below is really the same thing you posted. If you saw this code what would you think?

Code: Select all

$array = dynamic_function();
$x = count($array);
if($x > 0) {
  for($i = 0; $x > $i; $i++) {
  }
}
The only reason that you need the outer if() is for foreach() because foreach() will error on an empty array.
(#10850)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: to use an "if empty" statement, or not

Post by Ambush Commander »

The only reason that you need the outer if() is for foreach() because foreach() will error on an empty array.

Code: Select all

foreach (array() as $foo) {echo 'Called';}
More like non-array causes an error.
if you run the empty function on a blank array, it evaulates a LOT quicker than if you evaluate on an array with 1200+ records.
I'm not sure what you're comparing: empty($empty_array) and empty($big_array), or empty($empty_array) and count($empty_array)

Personally, as long as dynamic_function() never returns a non-array, I'd scratch the conditional, just to keep the code simple.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: to use an "if empty" statement, or not

Post by infolock »

Very cool, thanks a lot guys for the input!
daleyjem
Forum Newbie
Posts: 4
Joined: Sun Mar 09, 2008 11:32 am

Re: to use an "if empty" statement, or not

Post by daleyjem »

if i may, let me try to clarify...

take the following code:

Code: Select all

 
<?php
$array = array();
$array = dynamic_function();  // This could return 1200 array values, no values, 1 value, etc.
if(!empty($array)) {
  $x = count($array);
  for($i = 0; $i < $x; $i++) {
  }
}
?>
 
if it in fact was quicker to use the "if" statement before executing the for(), it would only save a small fraction of time if the array was empty. if however the array had 1 BILLION items (it could happen), then you are sacrificing much more than just a fraction of time with a check. which would you rather take?

still yet though... the original, un-stated argument said that the for() loop contained 3 different declarations ($i = 0; $i < $someCounter; $i++), whereas the "if" statement only one... thus the reason for it in the first place. but how do you know what's going on internally within that empty() call? what do you think PHP is doing behind the scenes to check if your passed value is empty, especially since PHP variables are ambiguous of type? you could possibly be talking about more than 3 executions, on top of the "if" statement itself. so is there in fact really a time saver on an empty array? i don't remember testing that one, but i'm curious to.

it might have been safer to use (as suggested above), since we're going to be using the count() anyway:

Code: Select all

 
<?php
$array = dynamic_function();
$x = count($array);
if($x > 0) {
  for($i = 0; $i < $x; $i++) {
  }
}
?>
 
i don't remember... did we find the count() to be more or less time than the empty()?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: to use an "if empty" statement, or not

Post by Christopher »

Ambush Commander wrote:

Code: Select all

foreach (array() as $foo) {echo 'Called';}
More like non-array causes an error.
You are correct, bad wording on my part. If the array var is undefined you get a Notice and Warning, if it is null you get a Warning.
(#10850)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: to use an "if empty" statement, or not

Post by infolock »

daleyjem wrote:

Code: Select all

 
<?php
$array = dynamic_function();
$x = count($array);
if($x > 0) {
  for($i = 0; $i < $x; $i++) {
  }
}
?>
 
We actually tested this, versus an empty check. The if($x > 0) check is actually slower than a basic empty() function check.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: to use an "if empty" statement, or not

Post by Ambush Commander »

Makes sense, since $x = count() requires more opcodes, and requires the allocation of another variable.
Post Reply