I wouldn't really use a while statement for this, but a for loop instead, as it 
needs less code:

<?php

echo "<select>";

for($i =1;$i<=12;$i++)
{
     $dateformat = date("M", mktime(0,0,0, $i,0,0));

     echo <<<HTML

     <option value="$i">$dateformat $i</option>

HTML;
}

echo "</select>";
?>


The other code was starting from 0 and going to 12, which is 13 months! Also, 
you had the variable being compared as a string, which will work in this 
particular case, because php will convert $i to a string before each comparison 
in the loop. While it works in this case, its best practice to use the variable 
type you need to avoid unusual behavior.

The mktime function is unusual in that indexes for months start at 1 and not 0 
as you might expect, but it does indicate that on the manual pages.

Thanks,
Ash
http://www.ashleysheridan.co.uk

----- Reply message -----
From: "Jason Pruim" <li...@pruimphotography.com>
Date: Sat, Oct 16, 2010 16:12
Subject: [PHP] odd while behavior...
To: "PHP General list" <php-general@lists.php.net>

Okay so I'm just playing around with some stuff trying to learn more  
and expand my knowledge and I ran into something I don't understand...  
Take the following code:

<?PHP

echo "<select>";
$i ="0";
     while($i <="12") {

     $dateformat = date("M", mktime(0,0,0, $i,0,0));
     $month = mktime(0,0,0, $i,0,0);
     //echo date("M", mktime(0,0,0, $i,0,0));
     //echo "<br>inside while<br>";
     echo <<<HTML
     <option value="{$i}">{$dateformat} {$i}</option>
HTML;
     $i++;
     }
echo "</select>";

?>

which does display a select drop down box with the month's in it...  
But on my test server it starts the display at december... With  
setting $i = "1" i would have thought it should start at january?

Any ideas on what I missed? :)


Thanks for looking! :)


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to