What's the Difference Between If(!$Variable) and If(Isset($Variable))

Difference between if(isset($var)) and if($var)

Using

if ($var)

You'll test if $var contains a value that's not false -- 1 is true, 123 is too, ...

For more informations about what is considered as true or false, you should take a look at Converting to Boolean.


Using isset(), you'll test if a variable has been set -- i.e. if any not-null value has been written to it.

What's the difference between if(!$variable) and if(isset($variable))?

Well, the answer is pretty simple. isset($var) returns whether or not a variable exists and is not null, where !$var tells you if that variable is true, or anything that evaluates to true (such as a non-empty string). This is summarized in the first table of this documentation page.

Also, using !$var will output a notice that you're using an undefined variable, whereas isset($var) won't do that.

Mind you, they are two different things:

<?php
var_dump( isset($foo) ); // false.
var_dump( !$foo ); // true, but with a warning.

$foo = false;
var_dump( isset($foo) ); // true
var_dump( !$foo ); // true.

What is the difference between if(isset($a)) and if($a) in php?

With $a = false;:

if ($a) {} will return false, whereas if (isset($a)) {} will return true.

I do not know that if_exist you speak of. :)

Edit: Please check @Utkanos's answer for an excellent and more expansive explanation. :)

Php if($_POST) vs if(isset($_POST))

isset determine if a variable is set and is not NULL. $_POST will always be set and will always be an array.

Without isset you are just testing if the value is truthy. An empty array (which $_POST will be if you aren't posting any data) will not be truthy.

Why check both isset() and !empty()

This is completely redundant. empty is more or less shorthand for !isset($foo) || !$foo, and !empty is analogous to isset($foo) && $foo. I.e. empty does the reverse thing of isset plus an additional check for the truthiness of a value.

Or in other words, empty is the same as !$foo, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.

The manual puts it like this:

empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

You can simply use !empty($vars[1]) here.

Difference between `if (isset($_SESSION))` and `if ($_SESSION)`?

According to PHP.net, isset() does the following:

Determine if a variable is set and is not NULL.

When writing:

<?php  if($_SESSION['username']) {...} ?>

You are checking to see if $_SESSION['username'] is equal to true. In other words, you are checking if the value does not equal false.

According to PHP.net the following are considered FALSE:

When converting to boolean, the following values are considered FALSE:

the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags

As you can see unset variables / NULL variables are considered FALSE. Therefore by testing if the $_SESSION element is true, you are also determining if it exists.

Isset, on the otherhand, actually checks if the variable exists. If you would like to know if there is a SESSION variable by that name, use isset() as testing it for TRUE/FALSE isn't dependent on whether the variable exists or not.

Further, look at the following examples:

$_SESSION['a'] = FALSE;
if($_SESSION['a']){
echo 'Hello'; //This line is NOT echo'd.
}

if(isset($_SESSION['b'])){
echo 'Hello'; //This line is NOT echo'd because $_SESSION['b'] has not been set.
}

In where shall I use isset() and !empty()

isset vs. !empty

FTA:

"isset() checks if a variable has a
value including (False, 0 or empty
string), but not NULL. Returns TRUE
if var exists; FALSE otherwise.

On the other hand the empty() function
checks if the variable has an empty
value empty string, 0, NULL or
False. Returns FALSE if var has a
non-empty and non-zero value."

JavaScript isset() equivalent

I generally use the typeof operator:

if (typeof obj.foo !== 'undefined') {
// your code here
}

It will return "undefined" either if the property doesn't exist or its value is undefined.

(See also: Difference between undefined and not being defined.)

There are other ways to figure out if a property exists on an object, like the hasOwnProperty method:

if (obj.hasOwnProperty('foo')) {
// your code here
}

And the in operator:

if ('foo' in obj) {
// your code here
}

The difference between the last two is that the hasOwnProperty method will check if the property exist physically on the object (the property is not inherited).

The in operator will check on all the properties reachable up in the prototype chain, e.g.:

var obj = { foo: 'bar'};

obj.hasOwnProperty('foo'); // true
obj.hasOwnProperty('toString'); // false
'toString' in obj; // true

As you can see, hasOwnProperty returns false and the in operator returns true when checking the toString method, this method is defined up in the prototype chain, because obj inherits form Object.prototype.

How exactly does if($variable) work?

The construct if ($variable) tests to see if $variable evaluates to any "truthy" value. It can be a boolean TRUE, or a non-empty, non-NULL value, or non-zero number. Have a look at the list of boolean evaluations in the PHP docs.

From the PHP documentation:

var_dump((bool) "");        // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)

Note however that if ($variable) is not appropriate to use when testing if a variable or array key has been initialized. If it the variable or array key does not yet exist, this would result in an E_NOTICE Undefined variable $variable.



Related Topics



Leave a reply



Submit