Re: PHP boolean's
- From: "Rik Wasmus" <luiheidsgoeroe@xxxxxxxxxxx>
- Date: Sat, 31 May 2008 14:37:07 +0200
On Sat, 31 May 2008 14:31:51 +0200, Michael Fesser <netizen@xxxxxx> wrote:
.oO(Michael Sharman)
I suppose I can also do;
if(!($count % 2))
Yes. But even if this is documented behaviour, it's usually recommended
to explicitly write what you want to test for. In this case you're not
testing a boolean value ('!' is a logical operator), but an arithmetic
expression. So you should better write it as
if ($count % 2 == 0) {
...
}
This is cleaner and more readable code. Another example: The typical
shorthand way for testing if an array is empty or not is
if (!$someArray) {
...
}
But the better way is
if (empty($someArray)) {
...
}
One could even argue using if(count($someArray) == 0) is better, as empty() will not only work for arrays , but for a whole list of empty values ('',0,null, etc.). Using the count function does immediatly issue an error (non-fatal) when $someArray is not an array, making it more easy to track bugs and errors. That being, said, I usually do use empty() though :).
--
Rik Wasmus
....spamrun finished
.
- Follow-Ups:
- Re: PHP boolean's
- From: Michael Fesser
- Re: PHP boolean's
- References:
- PHP boolean's
- From: Michael Sharman
- Re: PHP boolean's
- From: petersprc
- Re: PHP boolean's
- From: Michael Sharman
- Re: PHP boolean's
- From: Michael Fesser
- PHP boolean's
- Prev by Date: Re: PHP boolean's
- Next by Date: Re: PHP boolean's
- Previous by thread: Re: PHP boolean's
- Next by thread: Re: PHP boolean's
- Index(es):
Relevant Pages
|