RE: Alternate Colors in Rows ($r=!$r)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



[snip]
> 
> But why does the ($r=!$r) ternary condition work?. (I understand
> that it DOES but not WHY.)

Because he's rotating between boolean values.

    $r = true;
    $r = !$r;    // Now $r is false; 
    $r = !$r;    // Now $r is true;
    $r = !$r;    // Now $r is false; 
    $r = !$r;    // Now $r is true;
    $r = !$r;    // Now $r is false; 
    $r = !$r;    // Now $r is true;
    $r = !$r;    // Now $r is false; 
    $r = !$r;    // Now $r is true;
    ...
[/snip]

We just did that proof in the office as well. With a little echoing you
will see that when $r is TRUE it is set to 1, when it is false it is set
to NULL.

But it still should not work logically because you are performing an
assignment in the IF (it doesn't have to be ternary to work, that is
just elegant).

<?php
echo "<pre>";
echo ($r = !$r)?"Yes\n":"No\n";
echo $r."\n";
echo ($r = !$r)?"Yes":"No";
echo $r."\n";

if($r = !$r){
        echo "Yes\n";
}else{
        echo "No\n";
}
echo $r."\n";


if($r = !$r){
        echo "Yes\n";
}else{
        echo "No\n";
}
echo $r."\n";
echo "</pre>";
?>

Returns
Yes
1
No
Yes
1
No
Yes
1
No
Yes
1
No

It looks like PHP has an "unintentional feature". Doing this; if($r =
!$r) should always return TRUE because it is an assignment. I don't know
if I would rely on this.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux