PHP Functions Ex. #4: Function for HTML
For this PHP exercise, first create an array called $months. Use the names of the months as keys, and the number of days for each month as values. For February, use the following for your value: "28 days, if leap year 29".
Next, write a function to create an option element for a form's select field. Make sure each option will be upper case. Both the array and the function should precede the HTML for the page.
Once again, you will be requesting user input. Create a form for the user with the request, "Please choose a month." Next, provide a select field with the months as options, looping through the array you created and using the function to create the option elements.
When the user clicks the submit button, return the statement "The month of $month has $number days.", where $month is the name of the month the user chose, and $number is the number of days. Be sure to include a different response for February.
 
Comments
February
Quick question, I set my function up like this:
in the array, i pulled in my $months array, and called it with
in the middle of my form. everything worked, and when i hit submit the keys carried over like they should have. my problem was with the values, every one of them was 31. did i do something wrong with the hidden field?
also the br(); is a function I made for an html break.
Thanks.
yes
I was thinking the same way about the problem
Need help understanding some lines of code
had the same problem and ran into the other issue where the value for 'month' and 'days' were switched around in the echo statement.
If the original author is still monitoring these forums can you explain how the following function works in relation works?
Specifically how the function fills out the drop down (I understand the foreach portion which runs through the arrays) and supplies the number of days in the echo statement.
Many thanks.
February
wow my script is almost identical to the answer script!
I'm just wondering, why make another conditional statement for February? It works perfectly without it.
Thanks!
Two reasons
Two reasons why to include the conditional statement for Feb:
1. It's good practice.
2. The phrase sounds better without the second 'day'.
I see
thanks for the tip. now that you mentioned it, it really sounds better that way. ^_^
Simple html echo with $array [key]
Can someone tell me why the following syntax worked and the 2nd does not? seems the same to me.
THIS WORKS:
BUT THIS SYNTAX DOES NOT WORK:
echo "The month of $get_month has $month["February"] days.";
Error Message: Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /Applications/MAMP/htdocs/functions_ex4.php on line 96
What's the reason?
Need help understanding some lines of code
Need help understanding the code with the function portion expanding out the drop down list and how it displays the number of days in the second echo statement.
What are you passing into the function as $str? I am presuming it's the $key array value but am foggy on where this is done?
Need to declare/access array as GLOBAL !
I did not view the solution until i figure out whats causing my code not to work. My original code is almost the same as above but it did not work for me. I'm using PHP 5.4.16. I have to declare or access the variable as global for it to work. Here's my little code:
<?php
}
?>
<?php
}
}
?>
This site has to be one of the best site I've seen for PHP tutorial. It's more engaging thus you'll have fun while taking on the little challenges. Retention of knowledge acquired is is more effective this way.
Thank you so much!