richiejones24 Posted November 9, 2011 Share Posted November 9, 2011 Hi i am connecting to my database and i need the script to print something if the field is not empty, I am using the following, but its not working any ideas where i am going wrong? if (isset($row[image_zero])) { Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 if (!empty($row[image_zero])) { Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 9, 2011 Share Posted November 9, 2011 Both sets of code are incorrect: if (isset($row['image_zero'])) Array keys need to be quoted (like all strings). You're clearly working with error-reporting turned off otherwise you would have seen this error. For all we know, $row isn't even an array. -Dan Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 Both sets of code are incorrect: if (isset($row['image_zero'])) Array keys need to be quoted (like all strings). You're clearly working with error-reporting turned off otherwise you would have seen this error. For all we know, $row isn't even an array. -Dan oops, forgot the quotes.. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 9, 2011 Share Posted November 9, 2011 Both sets of code are incorrect: if (isset($row['image_zero'])) Array keys need to be quoted (like all strings). You're clearly working with error-reporting turned off otherwise you would have seen this error. For all we know, $row isn't even an array. -Dan oops, forgot the quotes.. Your solution also would have thrown an error if the row wasn't set. The actual solution is probably: if (!isset($row['image_zero']) || empty($row['image_zero'])) That depends on what $row is though. If it's a db query row, it's probably always SET. Quote Link to comment Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 isset should only be used on variables/keys created outside of your script (through forms, query string, etc) If you have to use isset() on internal variables/keys, then you don't have a tight enough grip on your application's design. <?php if( someCondition ) { $newVar = 'declared'; } ?> Is a bad idea. You're creating the variable only under certain conditions. Instead, you should declare it outside of any conditional statements, and give it a value conditionally <?php $newVar = NULL; if( someCondition ) { $newVar = 'declared'; } ?> That way, if you want to verify the variable's contents later, you don't have to worry about making sure it exists first. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 9, 2011 Share Posted November 9, 2011 Bad news: setting a variable to NULL doesn't set it, it unsets it: php > $a = null; php > if ( isset($a) ) echo "A IS SET\n"; php > $a = ''; php > if ( isset($a) ) echo "A IS SET\n"; A IS SET php > unset($a); php > if ( isset($a) ) echo "A IS SET\n"; php > $a = "abc"; php > if ( isset($a) ) echo "A IS SET\n"; A IS SET php > $a = null; php > if ( isset($a) ) echo "A IS SET\n"; php > Quote Link to comment Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 In the manual entry for isset it states isset — Determine if a variable is set and is not NULL Setting a variable to NULL != unsetting a variable. If you've declared it as null unconditionally, there's no need to call isset() on it. You know it exists. That was my point Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 9, 2011 Share Posted November 9, 2011 Oh PHP, why don't you work the way you should. isset() should be called Issetandnotnull() if that's the case. Quote Link to comment Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 Well, isset itself is a kind of stupid function. It's only needed because some (very smart) goofball decided we don't need to explicitly declare variables we want to use. The only time you should need to check if a variable actually exists is when it's being supplied from an outside source. isset() has it's place when using register_globals, but as that's been deemed 'bad' by the community, so should isset(). The only reason I use it still is because it's faster (lolwut?) than using array_key_exists(). Regardless, it's here, and we use it. Quote Link to comment 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.