|
Posted by Jerry Stuckle on 07/11/06 11:22
Rik wrote:
> David Haynes wrote:
>
>>Rik wrote:
>>
>>>David Haynes wrote:
>>>
>>>>$result = mysqli_query($sql);
>>>>while( $row = mysql_fetch_assoc($result) ) {
>>>>$isArchived = $row['is_archived'] ? true : false;
>>>>...
>>>>}
>>>
>>>
>>>$isArchived = (bool)$row['isarchived'];
>>>it's shorter, and by using $row['is_archived'] ? you're already
>>>evaluating
>>>the boolean value.
>>>
>>>
>>>
>
> http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
>
>>>Grtz,
>>
>>Rik,
>>$row['isarchived'] is already a 0/1 value (Tinyint(1)), so really the
>>(bool) cast is a no-op. My bad. You could just use $isArchived =
>>$row['isarchived']
>
>
> What do you mean by no-op? I'm no native speaker, please explain. And
> already 0/1? 0/1 is an integer, not a boolean.
>
> <?php
> $row['test'] = 1; //so, integer 1, can be cast to true
> $istrue = (bool)$row['test'];
> echo '$row[test] can be cast to ';
> echo ($row['test'])?'true':'false';
> $string = "\n%s is %sa boolean";
> printf($string,'$row[test]',($row['test']===true)? '' :'not ');
> printf($string,'$istrue',($istrue===true)? '' :'not ');
> ?>
>
> Grtz,
Rik,
No-Op is from Assembler - "No Operation", although it is more correctly
referred to as "noop". It's an instruction which does nothing but take
time and memory. Useful when you need to delay a couple of CPU cycles
for something else to occur.
And you are correct - the (bool) is needed if you want the value to be a
boolean. However, if you're only going to test it, you could leave it
as an integer 1/0 instead of true/false; the result would be the same.
Personally I prefer the boolean. But that's only my preference; in most
cases there's no technical reason to choose one over the other.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|