Jump to content
MakeWebGames

isset() || array_key_exists()


Recommended Posts

Posted

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?

Posted (edited)

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
Posted
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 :)

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

Posted

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

Posted
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 :/

Posted

yes in your case due to GLOBAL variable $_GET is predefined array type but some times if you are not sure about variable type then you should check first, i said about totally overview

you can use both but i prefer isset();

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