Warning: session_start() [function.session-start]: open(/tmp/sess_6of0lgppd378ol96sc9ul2ata4, O_RDWR) failed: Disk quota exceeded (122) in /home/vipozico/forum.tutorials7.com/qa-include/qa-app-users.php on line 143
Day of Week - PHP Task
settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Day of Week - PHP Task

+4 votes

Enter a day number [1…7] and print the day name (in English) or “Invalid Day!“.

Use an array of strings.

Examples:

day of week php task

asked in PHP category by user hues

3 Answers

+3 votes

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!';
}
answered by user john7
edited by user golearnweb
+2 votes

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!';
}
answered by user eiorgert
0 votes

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!';
}
answered by user icabe
...