Skip to content

Instantly share code, notes, and snippets.

@hirak
Created April 10, 2013 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirak/5356212 to your computer and use it in GitHub Desktop.
Save hirak/5356212 to your computer and use it in GitHub Desktop.
switch文の使いどころの悩み ref: http://qiita.com/items/af2b304650272410ec29
<?php
switch (true) {
case 0:
echo '数字の0';
break;
case '0':
echo '文字列の0';
break;
case '0.0':
echo '文字列の0.0';
break;
case true:
echo '真偽値のtrue';
break;
}
<?php
if ($_GET['hoge'] === 'foo' || $_GET['hoge'] === 'moo' || $_GET['hoge'] === 'soo') {
//...
} elseif ($_GET['hoge'] === 'baa' || $_GET['hoge'] === 'buu') {
//...
}
<?php
switch ($_GET['hoge']) {
case 'foo': case 'moo': case 'soo':
//...
break;
case 'baa': case 'buu':
//...
break;
}
<?php
if (in_array($_GET['hoge'], ['foo', 'moo', 'soo'], true)) {
//...
} elseif (in_array($_GET['hoge'], ['baa', 'buu'], true)) {
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment