Guest Posted June 9, 2013 Posted June 9, 2013 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? Quote
KyleMassacre Posted June 9, 2013 Posted June 9, 2013 I think with isset() it still works if it is blank so you may wanna check and make sure there is something in there. Im not too sure about array_key_exists thought Quote
Dominion Posted June 9, 2013 Posted June 9, 2013 (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 June 9, 2013 by Dominion 1 Quote
Guest Posted June 9, 2013 Posted June 9, 2013 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 :) Quote
KyleMassacre Posted June 9, 2013 Posted June 9, 2013 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 Quote
Dominion Posted June 9, 2013 Posted June 9, 2013 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. Quote
rockwood Posted June 10, 2013 Posted June 10, 2013 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 Quote
Guest Posted June 10, 2013 Posted June 10, 2013 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 :/ Quote
rockwood Posted June 10, 2013 Posted June 10, 2013 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(); Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.