Re: [PHP] distinguish between null variable and unset variable
How can I tell the difference between a variable whose value is null and a variable which is not set? // cannot use === null: ket% php -r '$null = null; var_dump(null === $null);' bool(true) ket% php -r 'var_dump(null === $unset);' bool(true) ket% // - cannot use isset() either: ket% php -r '$null = null; var_dump(isset($null));' bool(false) ket% php -r 'var_dump(isset($unset));' bool(false) ket% Although this is a good problem to lose time for I think it is pointless. Since assigning the null constant to a var is making PHP to pretend like it never existed. Is this not the point? Without this feature I can think many cases that isset or is_null would be useless. You could always assign to something the empty string '' and use empty() to check it as an alternative. BTW what are you trying to do? -- Thodoris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] distinguish between null variable and unset variable
Daniel Brown wrote: On Wed, Jan 21, 2009 at 20:27, Jack Bates ms...@freezone.co.uk wrote: How can I tell the difference between a variable whose value is null and a variable which is not set? Unfortunately, in PHP - like other languages - you can't. A variable is considered to be null if: * it has been assigned the constant NULL. * it has not been set to any value yet. * it has been unset(). I'm not in a position to test right now, but using Dan's logic I would turn it around and test for isset first and then is_null. This makes sense to me, but maybe it is flawed: if (isset($var) is_null($var)) { echo $var is set and is null; } Or maybe a function to return is the $var === null: function eq_null($var) { return (isset($var) is_null($var)) ? true : false; } -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] distinguish between null variable and unset variable
Shawn McKenzie wrote: Daniel Brown wrote: On Wed, Jan 21, 2009 at 20:27, Jack Bates ms...@freezone.co.uk wrote: How can I tell the difference between a variable whose value is null and a variable which is not set? Unfortunately, in PHP - like other languages - you can't. A variable is considered to be null if: * it has been assigned the constant NULL. * it has not been set to any value yet. * it has been unset(). I'm not in a position to test right now, but using Dan's logic I would turn it around and test for isset first and then is_null. This makes sense to me, but maybe it is flawed: if (isset($var) is_null($var)) { echo $var is set and is null; } Or maybe a function to return is the $var === null: function eq_null($var) { return (isset($var) is_null($var)) ? true : false; } Or something like this (dunno, just brainstorming): function setornull($var) { if (!isset($var)) { return false; } elseif (is_null($var)) { return null; } return true; } -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] distinguish between null variable and unset variable
On Thu, Jan 22, 2009 at 15:11, Shawn McKenzie nos...@mckenzies.net wrote: Or something like this (dunno, just brainstorming): function setornull($var) { if (!isset($var)) { return false; } elseif (is_null($var)) { return null; } return true; } Unfortunately, neither solution would work. isset() will return FALSE even for an instantiated and explicitly-defined NULL variable. -- /Daniel P. Brown daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Unadvertised dedicated server deals, too low to print - email me to find out! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] distinguish between null variable and unset variable
On Thu, Jan 22, 2009 at 15:12, Daniel Brown danbr...@php.net wrote: Unfortunately, neither solution would work. isset() will return FALSE even for an instantiated and explicitly-defined NULL variable. Forgot to mention that, in addition, is_null() will return TRUE for both explicitly-set NULL variables and undefined variables alike. -- /Daniel P. Brown daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Unadvertised dedicated server deals, too low to print - email me to find out! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] distinguish between null variable and unset variable
Daniel Brown wrote: On Thu, Jan 22, 2009 at 15:11, Shawn McKenzie nos...@mckenzies.net wrote: Or something like this (dunno, just brainstorming): function setornull($var) { if (!isset($var)) { return false; } elseif (is_null($var)) { return null; } return true; } Unfortunately, neither solution would work. isset() will return FALSE even for an instantiated and explicitly-defined NULL variable. Yes, damn it! -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] distinguish between null variable and unset variable
Daniel Brown wrote: On Thu, Jan 22, 2009 at 15:12, Daniel Brown danbr...@php.net wrote: Unfortunately, neither solution would work. isset() will return FALSE even for an instantiated and explicitly-defined NULL variable. Forgot to mention that, in addition, is_null() will return TRUE for both explicitly-set NULL variables and undefined variables alike. That's why I was testing isset() fist, however as you pointed out, that is crap also. :-( -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] distinguish between null variable and unset variable
Shawn McKenzie schrieb: Daniel Brown wrote: On Thu, Jan 22, 2009 at 15:12, Daniel Brown danbr...@php.net wrote: Unfortunately, neither solution would work. isset() will return FALSE even for an instantiated and explicitly-defined NULL variable. Forgot to mention that, in addition, is_null() will return TRUE for both explicitly-set NULL variables and undefined variables alike. That's why I was testing isset() fist, however as you pointed out, that is crap also. :-( $testvariable1; $testvariable2 = null; $testvariable3 = ''; var_dump($testvariable1); var_dump($testvariable2); var_dump($testvariable3); var_dump( isset( $testvariable1)); var_dump( isset( $testvariable2)); var_dump( isset( $testvariable3)); var_dump( is_null( $testvariable1)); var_dump( is_null( $testvariable2)); var_dump( is_null( $testvariable3)); NULL NULL string(0) bool(false) bool(false) bool(true) bool(true) bool(true) bool(false) Regards Carlos -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] distinguish between null variable and unset variable
On Wed, Jan 21, 2009 at 20:27, Jack Bates ms...@freezone.co.uk wrote: How can I tell the difference between a variable whose value is null and a variable which is not set? Unfortunately, in PHP - like other languages - you can't. A variable is considered to be null if: * it has been assigned the constant NULL. * it has not been set to any value yet. * it has been unset(). -- /Daniel P. Brown daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Unadvertised dedicated server deals, too low to print - email me to find out! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] distinguish between null variable and unset variable
On Wed, Jan 21, 2009 at 05:27:35PM -0800, Jack Bates wrote: How can I tell the difference between a variable whose value is null and a variable which is not set? // cannot use === null: ket% php -r '$null = null; var_dump(null === $null);' bool(true) ket% php -r 'var_dump(null === $unset);' bool(true) ket% // - cannot use isset() either: ket% php -r '$null = null; var_dump(isset($null));' bool(false) ket% php -r 'var_dump(isset($unset));' bool(false) ket% Oh I *love* this problem. I still haven't found the perfect solution for it. But since a lot of things in PHP float around as strings, I often use strlen(trim($var)) == 0 to determine the emptiness of a variable. But it all depends on what type of variable you expect to receive. I don't have this problem so much with methods and functions, since I specifically engineer them to give me exact results. But I get it when testing POST and GET variables from web pages. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] distinguish between null variable and unset variable
On Thu, Jan 22, 2009 at 12:21 AM, Paul M Foster pa...@quillandmouse.comwrote: On Wed, Jan 21, 2009 at 05:27:35PM -0800, Jack Bates wrote: How can I tell the difference between a variable whose value is null and a variable which is not set? // cannot use === null: ket% php -r '$null = null; var_dump(null === $null);' bool(true) ket% php -r 'var_dump(null === $unset);' bool(true) ket% // - cannot use isset() either: ket% php -r '$null = null; var_dump(isset($null));' bool(false) ket% php -r 'var_dump(isset($unset));' bool(false) ket% Oh I *love* this problem. I still haven't found the perfect solution for it. But since a lot of things in PHP float around as strings, I often use strlen(trim($var)) == 0 to determine the emptiness of a variable. But it all depends on what type of variable you expect to receive. I don't have this problem so much with methods and functions, since I specifically engineer them to give me exact results. But I get it when testing POST and GET variables from web pages. Paul -- Paul M. Foster I've replaced all my techniques for testing POST and GET data by the filter extension. http://php.net/filter -- Alexandre Gomes Gaigalas alexan...@gaigalas.net http://Alexandre.Gaigalas.Net
Re: [PHP] distinguish between null variable and unset variable
2009/1/21 Daniel Brown danbr...@php.net: On Wed, Jan 21, 2009 at 20:27, Jack Bates ms...@freezone.co.uk wrote: How can I tell the difference between a variable whose value is null and a variable which is not set? Unfortunately, in PHP - like other languages - you can't. A variable is considered to be null if: * it has been assigned the constant NULL. * it has not been set to any value yet. * it has been unset(). Actually, you can, but it's not terribly pretty. Check for the variable name as a key in the array returned from get_defined_vars(): ?php $foo = 0; $bar = null; $variables = get_defined_vars(); // Check for $foo, $bar, and $baz: foreach (array('foo', 'bar', 'baz') as $var) { if (!array_key_exists($var, $variables)) { echo \$$var does not exist in the current scope.\n; continue; } if (is_null($$var)) { echo \$$var exists and is null in the current scope.\n; continue; } echo \$$var exists and is not null in the current scope.\n; } ? Again, not that pretty, and it only checks the local scope, but it can be done. Regards, Torben -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php