Jump to content
MakeWebGames

isset() || array_key_exists()


Guest

Recommended Posts

Just curious, which do you prefer to use when using them with forms? I am finding myself using array_key_exists more and more instead of isset(), likewise when using $_GET.

Are there any advantages / disadvantages to using either one?

Link to comment
Share on other sites

Depends if you're working with NULL values or not. If you want something to be valid as NULL then isset() isn't acceptable.

 

$array = array('key1' => 'YAY', 'key2' => NULL);

var_dump(isset($array['key1']));             // true
var_dump(array_key_exists('key1', $array));  // true

var_dump(isset($array['key2']));             // false
var_dump(array_key_exists('key2', $array));  // true

Edited by Dominion
  • Like 1
Link to comment
Share on other sites

Depends if you're working with NULL values or not. If you want something to be valid as NULL then isset() isn't acceptable.

 

$array = array('key1' => 'YAY', 'key2' => NULL);

var_dump(isset($array['key1']));             // true
var_dump(array_key_exists('key1', $array));  // true

var_dump(isset($array['key2']));             // false
var_dump(array_key_exists('key2', $array));  // true

Thanks bud :)

Link to comment
Share on other sites

this reminds me because with NWE if I don't use isset() on a get or post I get an error so I wonder if array_key_exists() will also work for certain things

If you're doing things like -

if(!$_GET['SOMETHING']) {

 

You should be getting an error like this -

Notice: Undefined index

Most people who don't use isset/array_key_exists tend to have error reporting off.

Link to comment
Share on other sites

as far as i know about both functions ,both are having very small difference array_key_exists use after check it is an array or not (means we need to make sure it is in array format or not) but in isset we don't need to do anything else

(by isset directly you can check any type ).

i most like isset

and error reporting off is not good

Link to comment
Share on other sites

as far as i know about both functions ,both are having very small difference array_key_exists use after check it is an array or not (means we need to make sure it is in array format or not) but in isset we don't need to do anything else

(by isset directly you can check any type ).

i most like isset

and error reporting off is not good

By the things I was referring to, being a form and $_GET, they will always be arrays so your answer doesn't get me any closer :/

Link to comment
Share on other sites

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.   Paste as plain text instead

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