Jump to content

if (isset($row[image_zero]))


richiejones24

Recommended Posts

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

 

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 > 

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.