Re: PHP boolean's



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
.



Relevant Pages

  • Re: Macros in php
    ... From the PHP manual for empty: ... "The following things are considered to be empty: ... array() ... practice in PHP. ...
    (comp.lang.php)
  • Re: A test for an Array
    ... Dim tmpArray() ... You shouldn't be using ReDim inside a loop, and especially not like that as you are loosing the data that should be in the earlier elements. ... I'd suggest first dimensioning v to the same bounds as tmpArray, or alternatively using ReDim preserve, but realize that ReDim preserve is inefficient, causing the array to be copied each time. ... is an "empty" array. ...
    (microsoft.public.vb.general.discussion)
  • Re: Problem with Vectors and STL/ATL in Visual C++ 2005
    ... when it returns the empty variant. ... Move the computation of the past-the-end pointer to outside the array ... This doesn't cater for an empty container though, ... Always maintain an additional object in the array. ...
    (microsoft.public.vc.stl)
  • Re: Chris Pine tutorial assistance chapter 7 sort data without use of .sort method
    ... wordCheck) from words_array and compare wordCheck with the first word, ... value of wordCheck into a second array and delete ... until words_array is empty. ...
    (comp.lang.ruby)
  • Re: Average if ...
    ... Valko" wrote: ... and make sure column C isn't empty: ... Don't forget, array entered! ... Microsoft Excel MVP ...
    (microsoft.public.excel.misc)