+ 1

What is wrong with this PHP code? Please help!

<?php $today = "Sun"; switch ($today){ case "Mon" && "Tue" && "Wed" && "Thu" && "Fri": echo "Weekday."; break; case "Sat" && "Sun": echo "Weekend."; break; default: echo "Invalid day."; } ?> Why is the output of this code "Weekday." It is supposed to be "Weekend." And I can't find out what I did wrong please help!! Thanks in advance :-))

10th Jul 2018, 1:30 PM
Simar
Simar - avatar
6 Réponses
+ 1
First of all switch doesn't support this use this one case "Mon" : case "Tue" : . . . echo "weekday" ; break; default: echo "Invalid day" ; break;
10th Jul 2018, 2:24 PM
ᴋᵘⁿᵃˡ
ᴋᵘⁿᵃˡ - avatar
+ 3
Simar unfortunately, that won't work because those logical operators "Mon" && "Tue" && "Wed" .... is evaluated as a boolean True and not as you wanted it to be. That's why it outputs 'Weekday'. You need individual case statements for each day of the week. Here's a sample code: https://code.sololearn.com/w1ZBO8RGMxCf/?ref=app
10th Jul 2018, 1:58 PM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 1
hey hinanawi thanks for replying i just tried it but it still doesnt work
10th Jul 2018, 1:54 PM
Simar
Simar - avatar
0
replace every "&&" with "||" and see if that fixes it
10th Jul 2018, 1:52 PM
hinanawi
hinanawi - avatar
0
okay thank you Kunal!
12th Jul 2018, 3:17 AM
Simar
Simar - avatar
0
thank you jonathan!
12th Jul 2018, 3:18 AM
Simar
Simar - avatar