Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHP8で導入されるmatch式 / php8_match
Search
kuwacchi
September 25, 2020
Technology
350
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PHP8で導入されるmatch式 / php8_match
PHP8で導入されるmatch式についての発表した内容です。
kuwacchi
September 25, 2020
More Decks by kuwacchi
See All by kuwacchi
おすすめの技術書 LT会_リファクタリング(第2版)/LT_refactor
kuwacchi
0
22k
スモークE2Eテストで20年モノのシステムからバグを燻し出す方法 / smokeE2E
kuwacchi
0
2.2k
Other Decks in Technology
See All in Technology
カートの信頼性を担保するWireMockを使ったe2eテスト
ykagano
0
250
つくって納得、つかって実感! 大規模言語モデルことはじめ ver2.0
recruitengineers
PRO
4
1.6k
ホームラボ紹介
y_sera15
0
160
関東Kaggler会発表資料
takoi
1
220
【AG-UI × A2UI × MCP Apps】Generative UIをやさしく解説する
nrinetcom
PRO
1
120
モバイルアプリ開発概論2026
recruitengineers
PRO
5
580
FPGAが実現する遠方宇宙の高空間分解能天体撮影 -大型地上望遠鏡の視力を補正する「補償光学」とは?-
komei_mt
0
240
ガバメント AI 源内を地方自治体は活用できるのか可能性と課題、期待について
takeda_h
1
400
ブラウザ研修 2026
recruitengineers
PRO
6
980
【CEDEC2026】グラフィックスエンジニアのためのニューラルシェーディング入門
cygames
PRO
0
140
クラウドセキュリティ入門 ~安全なクラウド利用のための基礎知識~
lhazy
13
8.7k
第3回しろおびセキュリティスポンサーセッション
log0417
0
180
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.5k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
650
The agentic SEO stack - context over prompts
schlessera
0
860
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Skip the Path - Find Your Career Trail
mkilby
1
180
Building AI with AI
inesmontani
PRO
1
1.1k
Balancing Empowerment & Direction
lara
6
1.2k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
340
Accessibility Awareness
sabderemane
1
170
Information Architects: The Missing Link in Design Systems
soysaucechin
0
1.1k
Transcript
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. PHP8で導入される
match式 桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. 自己紹介 •氏名:桑原 健悟(くわはら けんご) •所属:株式会社ラクス
•お仕事:Mail Dealerの開発
#phpltjam ©2020 RAKUS Co., Ltd. 今日のお話 •https://tech-blog.rakus.co.jp/entry/20200917/php
#phpltjam ©2020 RAKUS Co., Ltd. 今日のお話 PHP8で導入されたmatch式について ・switchとの違いは? ・何が嬉しいの?
#phpltjam ©2020 RAKUS Co., Ltd. お品書き ・match式とは ・matchの特徴 ・値の代入が得意 ・さらばbreak忘れ
・条件忘れにエラーで気付ける ・厳密な比較
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. match式とは
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. match式とは switchのような分岐の構文 $paramの値で処理を分岐
#phpltjam ©2020 RAKUS Co., Ltd. switchとmatchの違い match switch 返り値 あり
なし breakの記述 不要 必要 各条件の処理 1行でしか書けない ブロックで記述可能 比較 厳密(===) 緩やか(==) どの条件にも当てはまらない場合 エラーになる そのまま実行される
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. match式の特徴
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. 値の代入が得意
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. switchやifは「文」、matchは「式」 • 文とは • 値を返さない
→ 各条件で代入が必要 • 式とは • 値を返す → 代入忘れが発生しにくい 直接、値の代入はできない 値の代入は1回でいい
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. さらばbreak忘れ
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. break忘れが起こり得ない • switch:明示的にbreakが必要 • match:breakが存在しない
→ break忘れが起こり得ない
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. 条件忘れにエラーで気付ける
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. switch ユーザの成績(score)ごとに コマンド($command)を発行 日次処理でコマンドを実行するプログラム
#phpltjam ©2020 RAKUS Co., Ltd. match • matchは条件の網羅性を検査し該当がなければエラーとなる
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. 厳密な比較
桑原 健悟
#phpltjam ©2020 RAKUS Co., Ltd. 厳密な比較 • 「数値だと思っていたら文字列であり思わぬ分岐に入った」がなくなる
#phpltjam ©2020 RAKUS Co., Ltd. 厳密な比較は常に嬉しい? • PHPの関数が厳密な型を意識した作りでないため、 思わぬバグとなる例を紹介
#phpltjam ©2020 RAKUS Co., Ltd. 厳密な比較は常に嬉しい? 結果:"jpgだよ" 結果:“defaultだよ"
#phpltjam ©2020 RAKUS Co., Ltd. 厳密な比較は常に嬉しい? preg_matchはtrueでなく、1を返す。 → match式ではtrue===1となり、該当してもケースに入らない
#phpltjam ©2020 RAKUS Co., Ltd. まとめ • match式はswitchに比べバグが入りにくい仕様 • 代入忘れしにくい
• break忘れがない • 条件忘れもエラーで気付ける • 厳密な比較は良い面も悪い面もある • 既存コードやPHPの関数の型を把握していないと思わぬバグに繋がる恐れも
#phpltjam ©2020 RAKUS Co., Ltd. ©2020 RAKUS Co., Ltd. ご清聴ありがとうございました
桑原 健悟