Re: change color
| From: | Evan Nemerson | Date: | Tue, 27 May 2003 05:25:41 +0000 |
| Subject: | Re: change color | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-149050@lists.php.net to get a copy of this message | ||
Yup. It's called the ternary conditional operator. Handly wee bastard, if i do
say so myself ;). Check out
http://php.net/manual/en/language.operators.comparison.php
for details.
http://php.net/manual/en/language.operators.arithmetic.php
for stuff on
modulus (%) operator
On Monday 26 May 2003 08:43 pm, David Otton wrote:
> On Mon, 26 May 2003 23:01:25 -0400, you wrote:
> >This was a question I've been kicking around myself. But Evan's answer
> >is a bit confusing to me, can someone clear it up? Evan, can you explain
> >it a bit better? Maybe show how it is supposed to look in the overall
> > code?
>
> Breaking it down slightly, compared to the other replies you've received:
>
> You start by labeling each row (1, 2, 3, 4, etc). Odd-numbered rows we
> colour white, even-numbered rows we colour green.
>
> We already have a convenient label for our rows (the variable $i in the
> original example), we just have to decide when it's odd and when it's even.
>
> Even numbers are divisible exactly by 2, odd numbers leave a remainder.
>
> To find the remainder after a division, we use the mod (%) operator.
>
> 2 % 2 = 0
> 3 % 2 = 1
> 4 % 2 = 0
> 5 % 2 = 1
> etc.
>
> if ($i % 2 == 1) {
> echo ("white");
> } else {
> echo ("green");
> }
>
> The ?: stuff is just short-hand for if/else.
--
Any belief worth having must survive doubt.
-Unknown