On Tue, 19 Aug 2003, fabrice wrote:

> Empty($var) ? DoSomethingIfNotSetOrNull() : DoOther();

That won't work.  empty() will return true if the variable is set to a
literal zero, false, blank, or null.  So for your use, it's actually 
worse than isset assuming your data can have zeros or blank values.

A more proper check for a pseudo variable_exists would be something like 
this:

if (isset($szSomeString) || is_null($szSomeString))
{
  // Stuff here.
}

Because as we all know, isset only fails if the variable doesn't exist, 
or if it's set to null.  So add in a check for null, and you get the 
same thing as variable_exists.  Or if you're a math person:

isset = variable_exists - is_null
variable_exists = isset + is_null

So as a binary check:

variable_exists = isset || is_null

And there you have it.  Unfortunately, since PHP will flag a notice if 
you even use an undeclared variable, you can't write your own PHP 
function to check this, since invoking your function on an undefined 
variable will cause PHP to complain.  So you'll have to use the "isset | 
is_null" check on every variable you want to check in this manner.

In which case I'd say add this to the PHP core language to compliment 
function_exists and method_exists, and so on, or add it yourself with a 
relatively simple patch.

To those saying that setting a variable to null is stupid, what about 
class variables that are uninitialized?

var $nCount = NULL;

This is programmatically equivalent to initializing a variable to an 
unknown value, since the class has not yet been instantiated.  Good 
practice is good practice, regardless of what the language provides.  
Sure I don't have to do this, but it makes for more readable code.

This is a moot point, of course; I initialized the variable, so calling 
isset to check for existence would be silly.

Some people have suggested using array_key_exists when using arrays,
which is fine as long as you never use the PHP StdClass object which all
database wrappers may return.  Or you can waste memory by calling
get_object_vars to treat a StdClass object like an array for the two
things you wanted to check out of the entire object.

Actually now that I think about it, the object-level functions are 
badly crippled compared to the vastness of the array_* routines, 
foreach, and so on, but that's a different discussion.

So to summarize:

1.) variable_exists($sVar) = isset($sVar) || is_null($sVar)
2.) variable_exists can not be written in PHP.
3.) Using nulls to initialize variables is not stupid.
4.) Thus, add variable_exists, or deal with annoying double-check
    on every variable you intend to use this way.

I'm ambivalent either way, but the fact that variable_exists basically
does the work of array_key_exists, isset+is_null, and can't be done on 
the user level without turning notices off, and ends up being a very 
simple c function, why not just add it?

-- 
Shaun M. Thomas                INN Database Administrator
Phone: (309) 743-0812          Fax  : (309) 743-0830
Email: [EMAIL PROTECTED]    Web  : www.townnews.com



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

Reply via email to