0

[link]http://php.net/manual/en/function.isset.php[/link]

Clearly states that "isset — Determine if a variable is set and is not NULL"?

I have been trying to use isset() to check values for NULL and it still appears yet when I use != NULL then it works. I think the fine line is in various instances but if anyone can confirm my conclusions:
1. $variable = "" > value SET
2. $variable = "a" > value SET
3. no definition > value NOT SET
4. $variable = 0 > value NOT SET
5. $variable = NULL > value NOT SET

any other examples?
Inparticular the 1. and 4. should be noted as I thought 1. would be NOT SET and 4. should be SET but it seems they are not through testing. Anyone confirm these as they dont seem logical to me?

Thanks, Regards X

PS: If anyone has anything else to add go ahead.

Edited by OmniX: n/a

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by diafol
0

I think what your looking for is the empty() function Below is an example.

<?php
$arr=array('','a',0,NULL);
echo '//empty<br>';
$i=1;
foreach ($arr AS $val) {
if (empty($val) {
echo $i.' passed<br>'; }
$i++;
}

echo '//isset<br>';
$i=1;
foreach ($arr AS $vals) {
if (isset($vals)) {
echo $i.' passed<br>'; }
$i++;
}

[link]http://php.net/manual/en/function.isset.php[/link]

Clearly states that "isset — Determine if a variable is set and is not NULL"?

I have been trying to use isset() to check values for NULL and it still appears yet when I use != NULL then it works. I think the fine line is in various instances but if anyone can confirm my conclusions:
1. $variable = "" > value SET
2. $variable = "a" > value SET
3. no definition > value NOT SET
4. $variable = 0 > value NOT SET
5. $variable = NULL > value NOT SET

any other examples?
Inparticular the 1. and 4. should be noted as I thought 1. would be NOT SET and 4. should be SET but it seems they are not through testing. Anyone confirm these as they dont seem logical to me?

Thanks, Regards X

PS: If anyone has anything else to add go ahead.

0

Thanks cwarn, http://php.net/manual/en/function.empty.php.
(How do you do the link brackets?)

The following things are considered to be empty:

* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)

So I think using empty() instead of isset() is a better option?

0

Re isset()
$variable = "" > value SET
This is different to NULL as it is technically a string - zero-length. To my mind NULL means not set/no value at all, so this equates to no definition. However just my thoughts.
A variable set to zero not showing up a set strikes me as odd as this should be a valid value. If this is true, can you unset a variable by setting the value to 0 as opposed to using unset()?

Oh, bugger. I've got a million scripts that use isset() checks. *Blood turns to ice*

1

I think the reason why 0 is not isset is because the binary value of zero is 00000000. So any variable/array with the binary value 00000000 is said to be not isset. That is why zero is unset.

Votes + Comments
thanx for putting me straight on that
0

I think the reason why 0 is not isset is because the binary value of zero is 00000000. So any variable/array with the binary value 00000000 is said to be not isset. That is why zero is unset.

Thanx C. *Frantically fumbling for a quick fix - there goes another weekend*

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.