5
28/10/2024 6:52 pm
Topic starter
Enter a day number [1…7] and print the day name (in English) or “Invalid Day!“.
Use an array of strings.
Examples:
3 Answers
4
28/10/2024 6:53 pm
You can use isset() - line 15. Also see line 3 for the user input 🙂
Here is my code:
<?php $desiredDay = intval(readline()) - 1; $days = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]; if (isset($days[$desiredDay])) { echo $days[$desiredDay]; } else { echo 'Invalid Day!'; }
3
28/10/2024 6:54 pm
I've used unset() for the 0 element of the array (to get rid of it)...
<?php $desiredDay = intval(readline()); $days = [ 'Dummy', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]; unset($days[0]); if (isset($days[$desiredDay])) { echo $days[$desiredDay]; } else { echo 'Invalid Day!'; }
2
28/10/2024 6:55 pm
Another one:
<?php $days = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]; $day = intval(readline()); if ($day >= 1 && $day <= 7) { echo $days[$day - 1]; } else { echo 'Invalid Day!'; }