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

 

  Quote

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

  Quote

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.

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.

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 > 

In the manual entry for isset it states

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

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.

Archived

This topic is now archived and is closed to further replies.

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