Jump to content

[SOLVED] Difference between isset and != NULL


SchweppesAle

Recommended Posts

The language construct isset() checks if a variable is set while a null comparison checks if some sort of value equals the special value "null".

 

right, but what exactly does "set" mean.  I understand NULL

Has a value other than NULL.

 

Ex:

$var = null; // is NOT set
while..
$var = ''; // is set

Link to comment
Share on other sites

It means that it has been defined somewhere. For instance, if the following is a complete script, the variable $foo is not set, but the variable $var is:

<?php
$bar = 'foo';
var_dump(isset($foo), isset($bar));

 

right, so I'm assuming isset($foo) will return false and isset($bar) will return true.

 

in a conditional statement I would say

 

if(isset($bar))
{
echo var_dump($bar);
}

 

how is that any different from the following though?

if($bar != NULL)
{
echo var_dump($bar);
}

 

Link to comment
Share on other sites

Right, but there is the important distinction that isset() works on undefined variables.

 

I guess that's true if I hadn't declared $bar at all. 

 

Still, I guess it comes down to performance if that's the case.  Should I avoid "!= NULL" condition statements and stick with isset() for now on?

Link to comment
Share on other sites

Right, but there is the important distinction that isset() works on undefined variables.

 

I guess that's true if I hadn't declared $bar at all. 

 

Still, I guess it comes down to performance if that's the case.  Should I avoid "!= NULL" condition statements and stick with isset() for now on?

 

Again, there are 2 different concepts:

 

"isset" -> does this symbol exist in the PHP symbol table. 

"NULL" -> does a matching symbol exist, however there is no associated value.

 

 

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.