You are here: Re: array keys « PHP Programming Language « IT news, forums, messages
Re: array keys

Posted by Steve on 08/22/07 14:02

"Ulf Kadner" <dr_logic@gmx.net> wrote in message
news:fah4vj$1vt$01$1@news.t-online.com...
| pakalk wrote:
|
| > $tmp = array_keys( $input );
| > echo $tmp[0], $tmp[2];
|
| Bad code!

not 'bad' per say...just doesn't explain much using variable names like
$tmp. i'm sure $tmp refers to something meaningful. so, use a variable name
that isn't 'magical'. helps later on when maintaining or enhancing the code.
that's especially true the more $tmp is used later in the code. i also hate
seeing multiple sql resources named $stmt1, $stmt2, etc.. it chaps my hide
having to make sense of that non-scense.

| You have always to check if array keys exists before accessing it!

not 'always'. it depends on your error settings and whether or not you plan
to take action if $input is not an array...like notifying the user. usually
is_array() in this context would only mean a certain state did or did not
exist for this user's data entry, i.e. has/not submitted data. however, he
could just as easily check $tmp[0] or $tmp[2] for that.

$input = is_array($input) ? $input : array($input);

or

$tmp = @array_keys($input);

suffices and only the former checks.

| There is no warranty that key 2 exists.

right, but that may just be what he's banking on.

| Otherwise you become a error when accessing invalid keys.

again, not necessarily.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация