You are here: Re: [PHP] my for loop's gone bananas.. has yours? « PHP « IT news, forums, messages
Re: [PHP] my for loop's gone bananas.. has yours?

Posted by Richard Lynch on 01/19/05 18:33

Tim Burgan wrote:
> 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?

Order Of Operations:
.. and + have equal precedence.

So they are evaluated left-to-right, in the order they appear.

So this line:

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

turns into:

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

which turns into (using $i = 2 as an example):
echo ('<option value="2005' + '2"');

which then PHP needs to add two things, neither of which really look like
numbers, but it does the best it can by looking at the front part of your
string, and yields:

echo 0 + 2;

> 0"0</option>
> 1" selected="selected"1</option>
> 2"2</option>

So all you need to change to keep most of your existing code is
parentheses around: ($today_year + $i)

However, you would have much cleaner code if you just did:

for ($year = $today_year; $year < ($today_year + 10); $year++){
}

At that point, all your " + $i" stuff just goes away, and life gets real
simple.

For your penance, re-read this page :-)
http://us2.php.net/manual/en/language.operators.php

--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this 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

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