Reply to RE: [PHP] my for loop's gone bananas.. has yours?

Your name:

Reply:


Posted by Mike Ford on 01/19/05 13:06

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@timburgan.com]
> 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@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация