This is my code:
<?php
$date = 2015-02-30;
$year = substr($date, 0, 4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);
?>
<select>
<?php
for ($i=1; $i < 31; $i++) { ?>
<option value="<?php echo $i; ?>" <?php if($day === $i){ echo "selected"; }; ?>><?php echo $i; ?></option>
<?php } ?>
</select>
For the option, the number 4 should be selected. Why doesn't it work? Thanks
Sorry, I already had this in a select statement
EDIT: See the code edit above. Maybe because the
After your edit, it looks like this is your main problem:
This is not what you think it is. It should be quoted like this:
Otherwise,
$date
is a not a string, it's a math expression that evaluates to(int) 1983
, sosubstr($date, 8, 2);
will evaluate tofalse
, not30
, and then obviously your option won't be selected.You need to wrap your code in a select statement!
An option statement wont work without the select tag around it: