Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
マジカルインクリメントと指数表記
Search
tsuda.a
December 08, 2024
Programming
300
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
マジカルインクリメントと指数表記
PHP の特徴的な機能の一つである文字列へのインクリメントに関する注意喚起と、指数表記という普段あまり目にしないリテラル表記についての紹介です。
tsuda.a
December 08, 2024
More Decks by tsuda.a
See All by tsuda.a
Git を GUI で使う話
tsudaahr
0
140
バックアップしていますか?
tsudaahr
0
170
RDB以前のファイル設計の話でもしようか(ぇ
tsudaahr
0
180
NPUわからん
tsudaahr
0
230
計算量オーダーの話
tsudaahr
1
490
クラウド初学者が抱える不安について
tsudaahr
0
470
キューとは何か
tsudaahr
0
310
等幅は死んだ(ぇ
tsudaahr
0
150
いくら眺めてもエラーの理由がわからないコードについて
tsudaahr
0
250
Other Decks in Programming
See All in Programming
Simple Storage Service(S3) is not simple
iwatsukayura
0
240
JRuby: Past, Present, and Future
headius
0
230
Verilogで学ぶCPU自作入門
uyuki234
8
4.1k
Domain-Driven Transformation
hschwentner
2
2.3k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
530
mrbgem 三角測量 開発
ogom
0
220
Are APIs Still Relevant in the AI Era?
soyuka
0
450
Java 27新機能 / Java 27 new features
kishida
2
210
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
470
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
200
複雑なUIコンポーネントとの ⻑い付き合い⽅:波及するバグからテスト戦略を経て、安全な再設計へ
mixplace
0
300
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
470
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
700
Test your architecture with Archunit
thirion
2
2.4k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
630
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
870
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Transcript
マジカルインクリメントと 指数表記 PHP勉強会in広島 vol.2 LTDD 2024-12 #1 @tsuda_ahr
PHP には • マジカルインクリメントという機能があるらしい
マジカルインクリメントとは • 文字列に対してインクリメント演算子を適用すること。
試してみる <?php $a = "2024A0"; for($i=0; $i<30; $i++) { $a++;
echo $a."¥n"; } ?>
結果 https://onlinephp.io/
便利! • ですよね?
よしもっとインクリメントしよう! <?php $a = "2024A0"; for($i=0; $i<300; $i++) { $a++;
echo $a."¥n"; } ?>
結果
何が起こった? • わかるひと?
結果をさかのぼってみる
なぜ? • 2024E0 の次が 2024E1 (=期待値) ではなく、2025 なのか?
指数表記 • 数値リテラル(定数)は、 2024×100 (10の0乗) という表記を 2024E0 と書くことができる。
つまり • 2024E0 は 2024×100 = 2024×1 = 2024 となる。
• 2024 をインクリメントすると、2025 になる。
気を付けよう。 • マジカルインクリメントを使用する場合は、うっかり数値で評価されてしまう 文字列にならないことを検証しておこう。 • そもそもマジカルインクリメントは使わないほうがトラブルがなくて良いか も?
ちなみに • PHP5 までは、16進も数値評価していました。 (PHP7から評価しないように変更)
試してみる <?php $a = "0AA"; for($i=0;$i<1000;$i++) { $a++; echo $a."¥n";
} ?>
PHP5 ではこうなる
PHP7 ではこう。
こちらからは以上です。 • ご利用は計画的に(ぇ
と、いいたいところですが・・・ • マジカルインクリメントは PHP 8.3 から非推奨らしいです。 https://www.php.net/manual/ja/migration83.deprecated.php
ということで、str_increment を使いましょう。 <?php $a = "2024A0"; for($i=0; $i<300; $i++) {
$a = str_increment($a); echo $a."¥n"; } ?>
指数として評価されません。
Any Question? • 間違いとかあればご指摘ください。