DooPHP IRC channel


No need to use isset and empty in the condition

This is for general web development discussions that are not related with DooPHP.
PHP, Flash, HTML, Javascript, Useful tools, etc.

No need to use isset and empty in the condition

Postby roman » Tue Sep 08, 2009 10:46 pm

PHP's empty() returns true if its argument isn't initialized. So, for example, doing the following is redundant and wastes time:

isset($data['comments']) && !empty($data['comments'])
roman
 
Posts: 442
Joined: Sat Aug 01, 2009 8:31 pm

Re: No need to use isset and empty in the condition

Postby leng » Wed Sep 09, 2009 5:18 am

roman, this will throw a notice in PHP if $data['comments'] is never defined before the use of empty($data['comments']

This will create a Notice
Code: Select all
//it's never defined anywhere before
if(!empty($data['comments'])){

}


This is perfect E_STRICT compliant
Code: Select all
if( isset($data['comments']) && !empty($data['comments']) ){

}


Having the conditional order does affect PHP too, NOTICE!
Code: Select all
if( !empty($data['comments']) && isset($data['comments']) ){

}
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: No need to use isset and empty in the condition

Postby roman » Thu Sep 10, 2009 12:29 am

Leng, please help me reproduce what you think should happen. Here's the code I am using to test this:

Code: Select all
<?php
error_reporting(E_ALL | E_STRICT);
echo empty($data['message']);
?>


All I get is 1.

I use PHP 5.2.9 on Windows Vista with XAMPP.
roman
 
Posts: 442
Joined: Sat Aug 01, 2009 8:31 pm

Re: No need to use isset and empty in the condition

Postby leng » Thu Sep 10, 2009 1:26 am

hey yea right. I was wrong.
What I said should be for other functions than empty()
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: No need to use isset and empty in the condition

Postby leng » Thu Sep 10, 2009 1:38 am

btw the following is not completely redundant and wastes time
Code: Select all
isset($data['comments']) && !empty($data['comments'])


isset is faster than empty (well isset only checks if the var is set or not)
and if the first condition is false (isset), the second (empty) won't be checked, because it's an AND &&
So you have a faster conditional logic here (if most of the time you know that $data['comments'] will not be set at all)
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: No need to use isset and empty in the condition

Postby roman » Thu Sep 10, 2009 2:57 am

I know about the order of evaluation of conditions in PHP. It's the same in C.

I agree that if most of the time you know that $data['comments'] is not be set, then doing both may be OK. However, is the difference in speed of those functions great enough to justify increasing amount of code and probability of an error? Maybe empty() does the same thing that isset() does at first? Have you tested the speed of both of them when the argument is not set?
roman
 
Posts: 442
Joined: Sat Aug 01, 2009 8:31 pm

Re: No need to use isset and empty in the condition

Postby leng » Fri Sep 11, 2009 9:00 am

No I didn't test personally but I think I have seen some benchmark some where...
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm


Return to General Web Development Topics

Who is online

Users browsing this forum: No registered users and 1 guest