3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function year_select($name,$start_year,$end_year=NULL,$selected_year=NULL,$html_attrs=array()){ $end_year = (isset($end_year))? $end_year : date('Y'); $html_attributes = ""; if(count($html_attrs)>0) { foreach($html_attrs as $attr=>$val){ $html_attributes.=" $attr='$val'"; } } $select = "<select name='$name' $html_attributes><option>Select</option>"; for($i=$end_year;$i>=$start_year;$i--) { $selected=(isset($selected_year) && $selected_year==$i)? " selected='selected' " : ""; $select.="<option value='$i' $selected>$i</option>"; } $select.="</select>"; return $select; } echo year_select("hi",2003,NULL,2006);
Output for 4.3.0 - 7.1.0
<select name='hi' ><option>Select</option><option value='2014' >2014</option><option value='2013' >2013</option><option value='2012' >2012</option><option value='2011' >2011</option><option value='2010' >2010</option><option value='2009' >2009</option><option value='2008' >2008</option><option value='2007' >2007</option><option value='2006' selected='selected' >2006</option><option value='2005' >2005</option><option value='2004' >2004</option><option value='2003' >2003</option></select>