-3

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

baileyJchoi
  • 473
  • 5
  • 17
  • 1
    Looks like it does work, as far as I can tell. – Don't Panic Feb 17 '17 at 20:51
  • It works for me. I just pasted your code and tried to run and it works fine. – Paulo Hgo Feb 17 '17 at 20:51
  • 1
    Do you have wrapped this code in select tags? – Rizier123 Feb 17 '17 at 20:51
  • Maybe you could be a bit more specific about how it doesn't work. – Don't Panic Feb 17 '17 at 20:52
  • Sorry pls see edit. I already had select statement – baileyJchoi Feb 17 '17 at 20:54
  • 1
    you running this has `http://localhost` or `file:///`? or hosted? I'm thinking more like `file:///`. Tell us, what does HTML source reveal? *Spidey sense* is tingling here too. – Funk Forty Niner Feb 17 '17 at 20:56
  • Can you view the page source for the portion of the HTML output affected by this code? If you're just refreshing the page to check this, it's possible that your browser may be retaining the previously selected value. Try loading the page in a new tab. – Don't Panic Feb 17 '17 at 20:57
  • *♫Tic, toc,... tic toc...♫* - so, what's the verdict on my above comment? Or are you saying *"Ahhhh Fred, yeah, it was `file:///`, my bad"* and don't want to admit it *lol!* - The suspense is killing me ;-) – Funk Forty Niner Feb 17 '17 at 21:00
  • Possible duplicate of [PHP code is not being executed, instead code shows on the page](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – Funk Forty Niner Feb 17 '17 at 21:02
  • I'll just kindly show myself out then. *"Yeah Fred, you do that."*. – Funk Forty Niner Feb 17 '17 at 21:03
  • oh, a new edit: so now the plot thickens then. Yeah, I'm out and I'll pass on this one. – Funk Forty Niner Feb 17 '17 at 21:04
  • Sorry everyone pls look at the edit in the code above. I think it is the 'substr' not running first. How can I fix that? – baileyJchoi Feb 17 '17 at 21:05
  • 1
    @baileyJchoi Please answer the comment by Fred -ii- : http://stackoverflow.com/questions/42307112/echo-in-for-loop#comment71769351_42307112 – Rizier123 Feb 17 '17 at 21:06
  • @Rizier123 where? – baileyJchoi Feb 17 '17 at 21:09
  • I linked to it and it is under your question. How does your address bar looks like in your browser? `file:///` or `localhost`? What is the html source code output? – Rizier123 Feb 17 '17 at 21:11
  • Plus, `$date = 2015-02-30;` guess what that is being interpreted as? `2015minus02minus30` it needs to be quoted `$date = "2015-02-30";` which is probably what started this whole mess from the beginning, had you shown us your full code from the get go. – Funk Forty Niner Feb 17 '17 at 21:16
  • @Rizier123 No idea what they're doing and if they'll even pay attention to ^^^ - I also voted to close from the opening of it too; "not working". – Funk Forty Niner Feb 17 '17 at 21:17

2 Answers2

1

You need to wrap your code in a select statement!

An option statement wont work without the select tag around it:

<html>

<body>

<select>  <!-- Start the select statement -->

<!-- Your Code -->
<?php
$num = 4;
for ($i=1; $i < 10; $i++)
{
    ?>
    <option value="<?php echo $i; ?>" <?php if($num === $i){ echo "selected"; }; ?>><?php echo $i; ?></option>
    <?php
}
?>
<!-- End your code -->

</select> <!-- End the select statement -->

</body>

</html>
Chad Fisher
  • 353
  • 4
  • 16
0

After your edit, it looks like this is your main problem:

$date = 2015-02-30;

This is not what you think it is. It should be quoted like this:

$date = '2015-02-30';

Otherwise, $date is a not a string, it's a math expression that evaluates to (int) 1983, so substr($date, 8, 2); will evaluate to false, not 30, and then obviously your option won't be selected.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80