0

I'm trying to autofill the date function for a dropdown. I thought it would be a fairly straightforward bit of code. I've searched online and fiddled with it for about an hour now and still no luck. Any assistance would be greatly appreciated. :)

$day = 1;
while ($day < 32) {
echo "<option value=/"$day/"> $day </option>";
$day++;
}
1
  • 1
    echo "<option value=\"$day\"> $day </option>"; Commented Apr 10, 2014 at 16:16

3 Answers 3

3

In order to escape quotes and double quotes, your slashes need to be backslashes like below.

$day = 1;
while ($day < 32) {
  echo "<option value=\"{$day}\"> $day </option>";
  $day++;
}

It's also a good idea to wrap variables that aren't appended to a string using . with curly braces {}. If you get in the habit, performing tasks like printing an array value will be much easier later on.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I'm new to coding still so I had forgotten about needing to use the other slash for escaping. :) This will definitely help me for the future!
1

Have you thought about using a for loop?

The for way:

for($day=1;$day<32;$day++)   
    echo "<option value=\"{$day}\">$day</option>";

Your while way:

$day=1
while($day < 32){

    echo "<option value=\"{$day}\">$day</option>";
    $day++;
}

1 Comment

Very nice! I like the compactness of the for loop. I had not thought of it as I'm only 1 week into my first PHP class. After the input of their birth date is finished, I'll be working on having it output the amount of days the person has been alive. (The assignment only called for assigning variables and showing them on the next page... I like to overachieve. >.> )
0

You could try doing this on your code. instead of using slashes to scape your double quotes, i dont think they're different but this looks alot cleaner than all those slashes

$day = 1;
while ($day < 32) {
echo  "<option value='".$day."'> $day </option>";
$day++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.