dennismonsewicz Posted January 28, 2010 Share Posted January 28, 2010 I have been trying to better understand error checking but was wondering what are the best practices for using isset() vs empty()? Thanks! Dennis Quote Link to comment https://forums.phpfreaks.com/topic/190152-when-to-use-isset-and-when-to-use-empty/ Share on other sites More sharing options...
schilly Posted January 28, 2010 Share Posted January 28, 2010 hmmm good question. I'd like to see a good answer. From the manual anyways: empty() The following things are considered to be empty: "" (an empty string) 0 (0 as an integer) "0" (0 as a string) NULL FALSE array() (an empty array) var $var; (a variable declared, but without a value in a class) Where as isset(): any of the above would be considered true except NULL I think or the variable not existing. I think it really depends what you are trying to compare. Quote Link to comment https://forums.phpfreaks.com/topic/190152-when-to-use-isset-and-when-to-use-empty/#findComment-1003263 Share on other sites More sharing options...
Alex Posted January 28, 2010 Share Posted January 28, 2010 You use them for just what they sound like they should be used for. isset is used if you want to see that a variable is set, and empty is to see if a variable isn't null. isset should be used when checking for variables that have a possibility to be unset (like when a form is submitted), because it's the only way to test if a variable is set without getting a notice if it isn't. $var = ''; if(isset($var)) // true { // ... } if(!empty($var)) // false { // ... } Quote Link to comment https://forums.phpfreaks.com/topic/190152-when-to-use-isset-and-when-to-use-empty/#findComment-1003264 Share on other sites More sharing options...
schilly Posted January 28, 2010 Share Posted January 28, 2010 form example. for checking for a submit I use isset() but if I want to see if a field is blank, I use empty(). Quote Link to comment https://forums.phpfreaks.com/topic/190152-when-to-use-isset-and-when-to-use-empty/#findComment-1003265 Share on other sites More sharing options...
gwolgamott Posted January 28, 2010 Share Posted January 28, 2010 Here's a good list of when, where , why and the why nots of using empty() http://www.zachstronaut.com/posts/2009/02/09/careful-with-php-empty.html Quote Link to comment https://forums.phpfreaks.com/topic/190152-when-to-use-isset-and-when-to-use-empty/#findComment-1003266 Share on other sites More sharing options...
dennismonsewicz Posted January 28, 2010 Author Share Posted January 28, 2010 Thanks everyone! I will check out that website mentioned Quote Link to comment https://forums.phpfreaks.com/topic/190152-when-to-use-isset-and-when-to-use-empty/#findComment-1003279 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.