RE: my for loop's gone bananas.. has yours?

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

 



To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm



> -----Original Message-----
> From: Tim Burgan [mailto:email@xxxxxxxxxxxxx] 
> Sent: 19 January 2005 06:19
> 
> I have a for loop to create a HTML combo box that displays 
> the 10 year 
> values, starting from today's year and incrementing until 10 years it 
> reached.
> 
> The output of the loop to the browser is weird.
> If anyone has time, can you please let me know where I screwed up?
> 
> 
> Here's my loop:
> 
>    echo '<select name="expire_year" size="1">';
>   
>    $numbOfYears = 10;
>    for ( $i = 0; $i < $numbOfYears; $i++ )
>    {
>       echo '<option value="'. $today_year + $i .'"';

. And + have the same precedence, so their order of evaluation is decided by
their associativity; according to the table at
http://php.net/operators#language.operators.precedence, they are left
associative, so the above is interpreted as:

        echo (('<option value="'. $today_year) + $i) .'"';

>   
>       if ( $_SESSION['createStudent_expireYear'] == $today_year + $i )
>       {
>          echo ' selected="selected"';
>       }
>                               
>       echo '>'. $today_year + $i .'</option>';

And this, similarly, will be:

        echo (('>'. $today_year) + $i) .'</option>';

Some ways of dealing with this are:

(1) explicitly parenthesize the addition:

        echo '<option value="'. ($today_year + $i) .'"';

(2) use the multiple parameter feature of echo:

        echo '<option value="', $today_year + $i, '"';

(3) recast the loop to calculate the end year in advance, which avoids
having to repeatedly do the addition:

     $numbOfYears = 10;
     for ( $y = $today_year, $end_year = $today_year+$numbOfYears;
              $y < $endYear; $y++ )
     {
        echo '<option value="', $y, '"';

     ... etc.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services, JG125, James
Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS,
LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211

-- 
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