Posted by JackM on 01/22/06 02:38
Geoff Berrow wrote:
> Message-ID: <bpadnUgDvvR9FU_enZ2dnUVZ_sSdnZ2d@comcast.com> from JackM
> contained the following:
>
>
>>I can follow most of what you did but there are two lines that are
>>foreign to me:
>>
>>$spaces=($lines%$cols>0)? 1 : 0;
>
> The question mark is the ternary operator. It's a shorter way of
> writing if/else. '$lines%$cols' divides $lines by $cols and gives the
> remainder. If there is a remainder the result will be greater than 0 and
> $spaces is set to 1. If there is no remainder it is set to zero.
>
> I could have written
>
> if($lines%$cols>0){
> $spaces= 1;
> }
> else{
> $spaces=0;
> }
>
>
>>$rows=floor($lines/$cols)+$spaces;
>
> If we divide $lines by $cols we get the number of rows. The function
> floor just rounds it down. So if you have 9 lines and 4 columns the
> answer would be two. In fact the answer is two until you have 12 lines.
> However, if it doesn't divide exactly you need another row. That being
> the case, $spaces is already set to one and so we have the correct
> number.
Thanks for the explanations. Caught me off guard because I never came
across the ? or floor in code before. Not only did you solve my problem
but you taught me something new at the same time. Your explanations are
now crystal clear to me. Much obliged.
[Back to original message]
|