RE: [PHP] Alternate Colors in Rows ($r=!$r)
| From: | Daevid Vincent | Date: | Fri, 05 Oct 2007 19:36:59 +0000 |
| Subject: | RE: [PHP] Alternate Colors in Rows ($r=!$r) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-262846@lists.php.net to get a copy of this message | ||
First, I am flattered for all the "elegant" comments.
Honestly, this is PHP 102 level stuff, so I don't see all the fuss.
Yes it is the ternary operator -- I'm a big fan of that one.
(http://us.php.net/manual/en/language.expressions.php)
In the below example, (while sloppy on my part), by NOT initializing the $r,
it defaults to Boolean 'false' (not true as in the example), therefore !$r
will be Boolean true. So this just keeps bouncing between the two each time.
http://us.php.net/manual/en/language.variables.php
"It is not necessary to initialize variables in PHP however it is a very
good practice. Uninitialized variables have a default value of their type -
FALSE, zero, empty string or an empty array."
<?php
echo ($unset_bool ? "true" : "false"); // false
$unset_int += 25; // 0 + 25 => 25
echo $unset_string . "abc"; // "" . "abc" => "abc"
$unset_array[3] = "def"; // array() + array(3 => "def") => array(3 =>
"def")
?>
Anyways, I think everyone has analyzed this to death, and it's time for
ya'll to get back to work! :) Just add it to your arsenal of 'snippets' and
move on with your lives. HAHAH. Peace out yo.
D.Vin
http://daevid.com
> -----Original Message-----
> From: Jay Blanchard [mailto:jblanchard@pocket.com]
> Sent: Friday, October 05, 2007 11:39 AM
> To: Robert Cummings; Jeff Cohan
> Cc: php-general@lists.php.net
> Subject: RE: [PHP] Alternate Colors in Rows ($r=!$r)
>
> [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
>
>