Jump to content

Differences between isset($_GET['']) and !$_GET['']


3raser

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.