[Решено] Day of Week - PHP Task

  

5
Старт на темата

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

3 Отговори
4

Можете да използвате 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

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

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!';
}
Споделете: