+ 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 :-))
6 Antworten
+ 1
First of all switch doesn't support this use this one
case "Mon" :
case "Tue" :
.
.
.
echo "weekday" ;
break;
default:
echo "Invalid day" ;
break;
+ 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
+ 1
hey hinanawi thanks for replying i just tried it but it still doesnt work
0
replace every "&&" with "||" and see if that fixes it
0
okay thank you Kunal!
0
thank you jonathan!