{"id":801,"date":"2007-11-19T01:00:00","date_gmt":"2007-11-18T16:00:00","guid":{"rendered":"https:\/\/ecoop.net\/memo\/2007-11-19-1.html"},"modified":"2007-11-19T01:00:00","modified_gmt":"2007-11-18T16:00:00","slug":"2007-11-19-1","status":"publish","type":"post","link":"https:\/\/www.ecoop.net\/memo\/archives\/2007-11-19-1.html","title":{"rendered":"\u30d0\u30b0\u30d1\u30bf\u30fc\u30f3 &#8211; switch \u306e\u30c7\u30fc\u30bf\u578b\u306f\u8907\u6570\u7a2e\u985e\u306b\u3059\u308b\u3068\u4e88\u671f\u3057\u306a\u3044\u7d50\u679c\u306b\u306a\u308b"},"content":{"rendered":"<p>\n\u307e\u305a\u306f\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u898b\u3066\u3001\u7d50\u679c\u3092\u4e88\u60f3\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002\n<\/p>\n<blockquote><p>&lt;?php<br \/>\n$result = true;<br \/>\nswitch($result){<br \/>\n&nbsp;&nbsp;case &#8220;error&#8221;:  \/\/ \u30a8\u30e9\u30fc<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;echo &#8220;failure&#8221;;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;exit;<br \/>\n&nbsp;&nbsp;case &#8220;ok&#8221;:     \/\/ \u6210\u529f<br \/>\n&nbsp;&nbsp;case true:     \/\/<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;echo &#8220;success&#8221;;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;exit;<br \/>\n&nbsp;&nbsp;default:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;echo &#8220;unexpected&#8221;;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;exit;<br \/>\n}<br \/>\n?&gt;\n<\/p><\/blockquote>\n<p>\u4e00\u898b\u3001\u3053\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068 success \u304c\u51fa\u529b\u3055\u308c\u308b\u3088\u3046\u306b\u601d\u3044\u307e\u3059\u304c\u3001\u5b9f\u969b\u306b\u5b9f\u884c\u3057\u3066\u307f\u308b\u3068\u610f\u5916\u306a\u3053\u3068\u306b failure \u304c\u51fa\u529b\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002 <br \/>\nPHP \u306e switch \u6587\u306f\u3001switch \u306e\u5f15\u6570\u3068 case \u3067\u6307\u5b9a\u3057\u305f\u5024\u3068\u3092<s>= \u3067\u306f\u306a\u304f) <\/s> \u3067\u30c1\u30a7\u30c3\u30af\u3057\u3066\u3044\u308b\u305f\u3081\u3001switch \u5f15\u6570\u304c\u8907\u6570\u306e\u7a2e\u985e\u306e\u578b\u306b\u306a\u308b\u5834\u5408\u306f\u578b\u5909\u63db\u306a\u3069\u306b\u3088\u308a\u4e88\u60f3\u5916\u306e\u5024\u3068\u4e00\u81f4\u3059\u308b\u3053\u3068\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u304c\u539f\u56e0\u3067\u3059\u3002<br \/>\n&nbsp;<br \/>\n\u5b9f\u969b\u3069\u306e\u5024\u304c\u4f55\u3068\u4e00\u81f4\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u306f\u4ee5\u4e0b\u306e\u691c\u8a3c\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3066\u307f\u308b\u3068\u5206\u304b\u308a\u307e\u3059\u3002\n<\/p>\n<blockquote><p>\/\/ \u691c\u8a3c\u30b3\u30fc\u30c9:<br \/>\n&lt;?php<br \/>\n\/\/ values to test<br \/>\n$targets = array(<br \/>\n&nbsp;&nbsp;&nbsp;0,<br \/>\n&nbsp;&nbsp;&nbsp;1,<br \/>\n&nbsp;&nbsp;&nbsp;true,<br \/>\n&nbsp;&nbsp;&nbsp;false,<br \/>\n&nbsp;&nbsp;&nbsp;null,<br \/>\n&nbsp;&nbsp;&nbsp;&#8220;&#8221;,<br \/>\n&nbsp;&nbsp;&nbsp;&#8220;0.00e5&#8221;,<br \/>\n&nbsp;&nbsp;&nbsp;&#8220;10f&#8221;,<br \/>\n&nbsp;&nbsp;&nbsp;&#8220;0f&#8221;,<br \/>\n&nbsp;&nbsp;&nbsp;array(),<br \/>\n&nbsp;&nbsp;&nbsp;array(1),<br \/>\n&nbsp;&nbsp;&nbsp;array(&#8220;key&#8221;=>&#8221;foo&#8221;),<br \/>\n);<br \/>\n&nbsp;<br \/>\n\/\/ &#8212;-<br \/>\nforeach($targets as $target){<br \/>\n&nbsp;&nbsp;if(is_bool($target))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$s = $target? &#8220;true&#8221;: &#8220;false&#8221;;<br \/>\n&nbsp;&nbsp;elseif(is_null($target))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$s = &#8220;null&#8221;;<br \/>\n&nbsp;&nbsp;elseif(is_string($target))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$s = &#8220;&#8216;{$target}'&#8221;;<br \/>\n&nbsp;&nbsp;else<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$s = $target;<br \/>\n&nbsp;&nbsp;echo $s .&#8221;(&#8220;.gettype($target).&#8221;)&#8221;.&#8221;:\\t&#8221;;<br \/>\n&nbsp;&nbsp;switch($target){ \/\/&#8221;0.00e5&#8243;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;case &#8216;a&#8217;: echo &#8220;case a&#8221;; break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;case &#8216;b&#8217;: echo &#8220;case b&#8221;; break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;case &#8216;0&#8217;: echo &#8220;case zero&#8221;; break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;case  10: echo &#8220;case int 10&#8221;; break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;case 0: echo &#8220;case int 0&#8221;; break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;default: echo &#8220;default&#8221;; break;<br \/>\n&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;echo &#8220;\\n&#8221;;<br \/>\n}\n<\/p><\/blockquote>\n<p>php 4.4.6 \u304a\u3088\u3073 php 5.2.3  \u3067\u8a66\u3057\u305f\u3068\u3053\u308d\u3001\u5b9f\u884c\u7d50\u679c\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002\n<\/p>\n<blockquote><p>0(integer):     case a<br \/>\n1(integer):     default<br \/>\ntrue(boolean):  case a<br \/>\nfalse(boolean): case zero<br \/>\nnull(NULL):     case int 0<br \/>\n&#8221;(string):     case int 0<br \/>\n&#8216;0.00e5&#8242;(string):       case zero<br \/>\n&#8217;10f'(string):  case int 10<br \/>\n&#8216;0f'(string):   case int 0<br \/>\nArray(array):   default<br \/>\nArray(array):   default<br \/>\nArray(array):   default\n<\/p><\/blockquote>\n<p>\u540c\u3058 empty \u5024\u3067\u3082 0, null, false, &#8220;&#8221; \u3067\u7d50\u679c\u304c\u5168\u304f\u7570\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<br \/>\n\u307e\u305f\u3001 == \u306b\u3064\u3044\u3066\u6319\u3052\u3089\u308c\u308b\u6570\u5024\u6587\u5b57\u5217\u306e\u6bd4\u8f03\u6642\u306e\u554f\u984c(&#8220;0.00e5&#8221; == &#8220;0&#8221; \u304c\u771f\u306b\u306a\u308b,\u306a\u3069)\u306e\u5f71\u97ff\u3082\u53d7\u3051\u3066\u3057\u307e\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n&nbsp;<br \/>\n\u3053\u306e\u554f\u984c\u306f\u3001\u578b\u30c1\u30a7\u30c3\u30af\u3092\u53b3\u5bc6\u306b\u3057\u305f\u308a\u3001strval(), intval() \u306a\u3069\u3067\u660e\u793a\u7684\u306b\u6587\u5b57\u5217,\u6570\u5024\u30ad\u30e3\u30b9\u30c8\u3092\u884c\u3044\u3001switch \u6587\u306e\u5f15\u6570\u306e\u578b\u304c\u4e00\u7a2e\u985e\u306b\u306a\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u3053\u3068\u3067\u3042\u308b\u7a0b\u5ea6\u56de\u907f\u3067\u304d\u307e\u3059\u3002<br \/>\n\u3057\u304b\u3057\u4f9d\u7136\u3068\u3057\u3066\u6570\u5024\u3068\u3057\u3066\u6709\u52b9\u306a\u6587\u5b57\u5217\u540c\u58eb\u306e\u6bd4\u8f03\u306e\u554f\u984c\u304c\u3042\u308b\u305f\u3081\u3001\u6570\u5024\u3092\u542b\u3080\u6587\u5b57\u5217\u306b\u3064\u3044\u3066\u6b63\u78ba\u3055\u304c\u5fc5\u8981\u306a\u5834\u5408\u3084\u3001\u672c\u5f53\u306b\u8907\u6570\u578b\u3092\u5224\u5225\u3055\u305b\u305f\u3044\u5834\u5408\u306f<s>= \u6f14\u7b97\u5b50\u3092\u4f7f\u3044 if &#8211; elseif &#8211; else \u3092\u4f7f\u3046\u307b\u3046\u304c\u8ce2\u660e\u3067\u3057\u3087\u3046\u3002\n<\/p>\n<blockquote><p>if($result <\/s>= true || $result<s>= &#8220;ok&#8221;){<br \/>\n&nbsp;&nbsp;echo &#8220;success&#8221;;<br \/>\n}elseif(empty($result) || $result <\/s>= &#8220;error&#8221;){ <br \/>\n&nbsp;&nbsp;\/\/ empty($result) \u306f\u4ee5\u4e0b\u306e\u5f0f\u3068\u7b49\u4fa1\u3002<br \/>\n&nbsp;&nbsp;\/\/ (  !isset($result) || ($result<s>= false || $result <\/s>= null || <br \/>\n&nbsp;&nbsp;\/\/    $result<s>= &#8220;&#8221; || $result <\/s>= 0  )<br \/>\n&nbsp;&nbsp;echo &#8220;failure&#8221;;<br \/>\n}else{<br \/>\n&nbsp;&nbsp;echo &#8220;unexpected&#8221;;<br \/>\n}\n<\/p><\/blockquote>\n<p>&nbsp;<br \/>\n\u53c2\u8003:<br \/>\n&#8211; <a href=\"http:\/\/jp2.php.net\/manual\/ja\/control-structures.switch.php\">switch \u69cb\u6587<\/a>(php.net)<br \/>\n&#8211; <a href=\"http:\/\/jp.php.net\/manual\/ja\/language.operators.comparison.php#language.operators.comparison.ternary\">\u6bd4\u8f03\u6f14\u7b97\u5b50<\/a>(php.net)<br \/>\n&#8211; <a href=\"http:\/\/jp.php.net\/manual\/ja\/function.strval.php\">strval()<\/a>(php.net)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u307e\u305a\u306f\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u898b\u3066\u3001\u7d50\u679c\u3092\u4e88\u60f3\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002 &lt;?php $result = true; switch($result){ &nbsp;&nbsp;case &#8220;error&#8221;: \/ &hellip; <a href=\"https:\/\/www.ecoop.net\/memo\/archives\/2007-11-19-1.html\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">\u30d0\u30b0\u30d1\u30bf\u30fc\u30f3 &#8211; switch \u306e\u30c7\u30fc\u30bf\u578b\u306f\u8907\u6570\u7a2e\u985e\u306b\u3059\u308b\u3068\u4e88\u671f\u3057\u306a\u3044\u7d50\u679c\u306b\u306a\u308b<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[32],"class_list":["post-801","post","type-post","status-publish","format-standard","hentry","tag-php"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/comments?post=801"}],"version-history":[{"count":0,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/801\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/media?parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/categories?post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/tags?post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}