3raser Posted April 26, 2012 Share Posted April 26, 2012 A few months ago, and a good amount of time before that, I had people telling me to use isset() instead of performing to see if the variable is empty, such as: !$_GET[''] I know the differences in the function and what they do, but when could isset() be used in a situation where it's better/more efficient then: !$_GET[''] I do use isset(), though. Quote Link to comment https://forums.phpfreaks.com/topic/261619-differences-between-isset_get-and-_get/ Share on other sites More sharing options...
Psycho Posted April 26, 2012 Share Posted April 26, 2012 PHP is a loosely types language. For example the number 0 will be treated as equivalent to the string '0' or the logical FALSE Boolean, etc. But, it can be problematic to rely upon these type of comparisons. If the value of a variable is 0 or '0' and you used (!$var) the condition would be true - even though the value was not an empty string. It is much better to be explicit in these situations to prevent problems. So, if you want to know if a certain variable has been set or not, then you use isset(). If you want to know if the var is an empty string then do that comparison. But, there is another problem with using !$var as well - especially with $_GET vars. Depending upon the error reporting level on the server, trying to use if(!$_GET['var']) would produce warning messages on the page if the value was not set. Quote Link to comment https://forums.phpfreaks.com/topic/261619-differences-between-isset_get-and-_get/#findComment-1340598 Share on other sites More sharing options...
scootstah Posted April 26, 2012 Share Posted April 26, 2012 You can also use array_key_exists which is basically the same as isset() (for array's) except that it will return true even if the value of the key is null. Quote Link to comment https://forums.phpfreaks.com/topic/261619-differences-between-isset_get-and-_get/#findComment-1340659 Share on other sites More sharing options...
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.