Lester Caine wrote on 18/09/2015 12:38:
For SQL data coming from any decent database each variable can either
have a value in, ( which may be an empty string ) or be NULL. The NUMBER
of fields returned does not matter, a query returning a single record
can have a variable
Again, I know what NULLs are, and how they can come about; but you made
it sound like you could run an SQL query and have the first row having 6
columns, and the second having 5, which is not something I've ever seen.
(Substitute "record" or "tuple" for "row", and "fields" for "columns",
if you prefer.)
The number of fields returned DOES matter, because if the same number of
fields are returned in every case, then the same variables will be
defined in every case, and there would be no case where exists() would
return false. But I guess I'll take your word for it that there's some
way that the number of fields can vary.
so the third condition is that rather than a field
being NULL, it does not exist, so testing for isset() only works for a
field that 'is set' and empty() only works with an empty value.
Sure, sure, I understand that, really, I do.
Checking for the field being present but NULL needs is_null() but THAT throws a
notice for those fields which have not been returned in this particular
result set.
Your problem is not "is_null() throws notices", because that would lead
to "can I have a version of is_null() which doesn't throw notices?" to
which the answer is "yes, use isset()". The null condition is the one
which you CAN detect with current functions; it's the "not set"
condition which you can't detect, because neither is_null() nor isset()
will actually affirm the "present but" bit of your condition. That's not
is_null()'s fault, it's behaving the same way as the rest of the
language in assuming every uninitialised variable has a null value.
We are not talking about the number of states stored IN a variable
OK, maybe your example just wasn't a very good choice then, because
"Vehicle is not old enough to need MOT" feels very much like a state
being stored somewhere to me. That it is *represented by* a non-existent
variable is incidental, all you need is *some way of representing it*.
The date
value can be a valid date ( and genealogical date goes back before most
'empty' date values ) or NULL indicating no date set OR the variable can
be missing from the result set. If you add in 'empty' one might say we
have four distinct states?
A PHP variable can have all sorts of states, because you can take
advantage of loose typing:
$date = new DateTime();
$date = null;
$date = 'TODAY';
$date = -1;
$date = M_PI;
etc
Those are all readily distinguishable, and don't require any changes to
the language for you to base your code around them.
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php