奇怪的 switch 语句行为
有人可以告诉我为什么这个 switch
语句的输出与 if
语句不同吗? 更重要的是,我如何让它们输出相同的结果?
切换
switch ($list_day) {
case $today :
$calender .= '<td class="today">';
default :
$calender .= '<td>';
}
如果
if ($list_day == $today) {
$calendar.= '<td class="today">';
} else {
$calendar.= '<td>';
}
Can someone show me why this switch
statement doesn't output the same as the if
statement?
And more important, how do I get them to output the same?
Switch
switch ($list_day) {
case $today :
$calender .= '<td class="today">';
default :
$calender .= '<td>';
}
If
if ($list_day == $today) {
$calendar.= '<td class="today">';
} else {
$calendar.= '<td>';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您缺少
break
语句:如果您没有跳出 switch 语句,则执行将继续执行下一个标签。这允许您将标签链接在一起:
You are missing your
break
statements:If you don't break out of the switch statement execution just continues with the next label. This allows you to chain labels together:
您有一个拼写错误:
除了我之前提交的中断之外,您还输入了 $calender 而不是 $calendar...
You have a typo:
As well as the break i previously submitted, you typed $calender instead of $calendar...
你忘记了 BREAK;
在 Pascal 风格代码(例如 pascal、Visual Basic)中,您不需要对每种情况进行 BREAK。但在 C 风格/Java/PHP/等中,你需要:
这可以用来做一些奇特的事情,我会让你自己弄清楚!
You forgot your BREAK;
In Pascal style code such as pascal, visual basic, you don't need to BREAK on each case. But in C-Style/Java/PHP/etc you need:
This can be used to do some fancy things that i'll let you figure out yourself!