Intro:The switch-case statement in php

本文介绍了 PHP 中 switch-case 语句的基本用法及其与 if-elseif-else 语句的区别。通过示例代码展示了如何使用 switch-case 语句来处理多种情况,并解释了 break 语句的作用及 default 分支的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

To be continued,the switch-case statement comes.
An alternative to the if -elseif-else statement is the switch-case statement,which does almost the same thing:it tests a variable against a series of values until if finds a match,and then executes the code corresponding to that match.Consider the following code listing,which is equivalent to the preceding one:
<?php
//handle multiple possibilities
//define a different message for each day
$today='Tuesday';
switch($today){
 case 'Monday':
   echo 'Monday/'s child is fair of face.';
   break;
case 'Tuesday':
   echo 'Tuesday/'s child is full of grace.';
   break;
case 'Wednesday':
  echo 'Wednesday/'s child is full of woe.';
  break;
case '':......
default:
   echo 'No information available for that day';
   break;
}
?>
--------------------------------------------------------------------------------
The switch-case construct differs from the if-elseif-else construct in one important way.Once PHP finds a case statement that evaluates to true,it executes not only the code corresponding to that case statement,but also the code for all subsequent case statements.If this is not what you want,add a break statement to the end of each case block(as is done in the previous listing)to tell PHP to break out of the switch-case statement block once it executes the code corresponding to the first true case.
Notice also the 'default' case:as the name suggests,this specifies the default set of actions PHP should take if none of the other cases evaluate to true.This default case,like the else branch of the if-elseif-else block,is very useful as a "catch-all" handler for unforeseen situations.
(add-to-list 'magic-fallback-mode-alist '(buffer-standard-include-p . c++-mode)) (c-add-style "my-style" '("stroustrup" (indent-tabs-mode . nil) ; use spaces rather than tabs (c-basic-offset . 8) ; indent by four spaces (c-offsets-alist . ( (inline-open . 0) ; custom indentation rules (brace-list-open . 0) (statement-case-open . +) ))) ) (defun my-c++-mode-hook () (c-set-style "my-style") ; use my-style defined above (auto-fill-mode) ;(c-toggle-auto-hungry-state 1) (c-toggle-electric-state 1) ;(c-toggle-auto-newline 1);这个就是遇到了{};之后,自己开新行的 (c-toggle-hungry-state 1) ;(c-toggle-syntactic-indentation 1) ;(define-key c++-mode-map (kbd ":") 'semantic-ia-complete-symbol-menu) ;(define-key c++-mode-map (kbd ".") 'semantic-ia-complete-symbol-menu) (define-key c++-mode-map (kbd "C-c C-c") 'kill-ring-save);copy (define-key c++-mode-map (kbd "C-v") 'yank);paste (define-key c++-mode-map (kbd "C-x x") 'kill-region);cut ;(define-key c++-mode-map (kbd "C-c C-c") 'kill-ring-save);copy ;(define-key c++-mode-map (kbd "C-v") 'yank);paste (setq tab-width 8 indent-tabs-mode nil) ) (add-hook 'c++-mode-hook 'my-c++-mode-hook) (add-hook 'objc-mode-hook 'my-c++-mode-hook) ;--------------------------------传说中的施特劳斯特卢普的C++代码风格 --------------------------------------- ;----------------------------------C风格的对齐----------------------------- (defconst my-c-style '((c-tab-always-indent . t) (c-comment-only-line-offset . 4) (c-hanging-braces-alist . ((substatement-open after) (brace-list-open))) (c-hanging-colons-alist . ((member-init-intro before) (inher-intro) (case-label after) (label after) (access-label after))) (c-cleanup-list . (scope-operator empty-defun-braces defun-close-semi)) (c-offsets-alist . ((arglist-close . c-lineup-arglist) (substatement-open . 0) (case-label . 4) (block-open . 0); (defun-block-intro . 0) (statement-block-intro . 8) (substatement . 8) (knr-argdecl-intro . -))) (c-echo-syntactic-information-p . t) ) "My C Programming Style" ) ;; Customizations for all of c-mode, c++-mode, and objc-mode
05-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值