Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » PHP
  • » [PHP-DEV] Detecting if a null variable exists? [RSS Feed]

#1 June 11, 2010 19:04:45

Hannes M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Detecting if a null variable exists?


On Fri, Jun 11, 2010 at 19:43, Brian Moon <br***@*oonspot.net> wrote:
> Is it just me or are we missing a way in the language to check if a variable
> that has been set to NULL exists or not?
>
> is_null() on an unset variable throws a NOTICE.
> $var === null throws a notice.
>
> So, you have to use isset()? But, ah,
>
> $var = null;
> if(isset($var))
>
> yields false.
>
> Is array_key_exists("var", $GLOBALS) the only solution to this problem?


This question creeps up every once in a while, and I have seen
bunchload of code that uses array_key_exists("foo", $array); rather
then isset($array) because of it.

I however have never seen a proper usecase..
I tend to "initialize" my variables, and array keys, as null, so I
never have to do these kind of checks. And if you don't initialize
your variables, then you can turn of E_NOTICE if it bothers you.

-Hannes

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#2 June 12, 2010 12:08:07

Frederic H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Detecting if a null variable exists?


Hello !Is it just me or are we missing a way in the language to check if avariablethat has been set to NULL exists or not?

is_null() on an unset variable throws a NOTICE.
$var === null throws a notice.

So, you have to use isset()? But, ah,

$var = null;
if(isset($var))

yields false.

Is array_key_exists("var", $GLOBALS) the only solution to this problem?This question creeps up every once in a while, and I have seen
bunchload of code that uses array_key_exists("foo", $array); rather
then isset($array) because of it.

I however have never seen a proper usecase..class foo {
public function __call($method, $arguments)
{
// We MUST have at least ONE argument !if (array_key_exists(0, $arguments) === false) //isset($arguments) return false if first argument has null value{trigger_error('First argument of method ' . $method . '()must be defined', E_USER_ERROR);}
...
}
}

$foo = new foo();
$foo->magicCallToMethod(null); // no error
$foo->magicCallToMethod(); // error

Best regards,
Fred

--
========================================================================
Frédéric Hardy : Architecte d'application/Admin. système/Ergonome
========================================================================

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#3 June 14, 2010 13:09:40

Christian S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Detecting if a null variable exists?


Frederic Hardy wrote:
>> I however have never seen a proper usecase..
>
> class foo {
> public function __call($method, $arguments)
> {
> // We MUST have at least ONE argument !
> if (array_key_exists(0, $arguments) === false) //
> isset($arguments) return false if first argument has null value


Why not simply
if (count($arguments) == 0)
..
which is simpler and clearer?

I agree with Hannes that most "usecases" should be solved differently
anyway.

- Chris

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

  • Root
  • » PHP
  • » [PHP-DEV] Detecting if a null variable exists? [RSS Feed]

Board footer

Moderator control

Enjoy the 14th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support