[dev] Re: [cvs] commit: horde/lib String.php
Amith Varghese
amith at xalan.com
Tue Jan 28 13:31:02 PST 2003
> Even better would be if you also reflect the different behaviours of
> empty(), isset() and array_key_exists() and point out (based on Chucks
> comments) when to use which.
Well I thought the description was pretty good already (see below), but I'll
make one more change to the array_key_exists() stanza. I'll make it more clear
that it should be used when checking if the key is defined but its OK to be
false or null.
==========================================================================
> a. If you need to know if a variable exists at all, use isset():
[snip]
> b. If you need to know if a variable exists AND has a non-empty value,
> use !empty():
[snip]
> As pointed out in the comment of the else clause, empty() essentially
> does the same check as isset() - is this variable defined in the
> current scope? - and then, if it is, returns what the variable would
> evaluate to as a boolean. This means that 0, while potentially valid
> input, is "empty" - so if 0 is valid data for your case, don't use
> !empty().
-------------- next part --------------
Index: horde/docs/CODING_STANDARDS
===================================================================
RCS file: /repository/horde/docs/CODING_STANDARDS,v
retrieving revision 1.60
diff -r1.60 CODING_STANDARDS
492c492,493
< c. If you need to know if an array key is set, use array_key_exists():
---
> c. Use array_key_exists() when you want to check if an array key is
> defined but it's OK if the value is false or null:
494c495,496
< // Make sure we have a charset parameter.
---
> // Make sure we have a charset parameter. Value could be false
> // or null
497a500,503
>
> Please note that array_key_exists() is a performance hit (25%-100%)
> and should only be used when necessary. Instead try to use !empty()
> or isset() instead.
More information about the dev
mailing list