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
150
バックアップしていますか?
tsudaahr
0
170
RDB以前のファイル設計の話でもしようか(ぇ
tsudaahr
0
180
NPUわからん
tsudaahr
0
240
計算量オーダーの話
tsudaahr
1
490
クラウド初学者が抱える不安について
tsudaahr
0
480
キューとは何か
tsudaahr
0
310
等幅は死んだ(ぇ
tsudaahr
0
160
いくら眺めてもエラーの理由がわからないコードについて
tsudaahr
0
250
Other Decks in Programming
See All in Programming
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
350
作って学ぶSpring BootのAuto Configuration / JJUG Night Seminar 2026-10-19
rshindo
0
220
私なりのハーネスエンジニアリング ~独自の言語処理系の開発を通して~
seosoft
0
160
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
1
790
Crash Auto Pipelines
veronikapj
1
300
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
250
Groovy as a human and AI friendly programming language
paulk
0
150
No More NoSuchMethodError
exoego
1
120
Performance Optimization With LLMs
xleitix
0
230
[Dev Group Paris Octobre 26] Agentforce, SIforce, Claudeforce, Slackbot… on s'y retrouve ?
nabondance
0
120
[DroidKnights 2026] Workflow AX: From Auto-Documented Policies to E2E Testing
raeminkang
0
230
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
4
3.5k
Featured
See All Featured
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
280
Ethics towards AI in product and experience design
skipperchong
2
450
Are puppies a ranking factor?
jonoalderson
2
4.1k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7k
First, design no harm
axbom
PRO
2
1.4k
How to Think Like a Performance Engineer
csswizardry
28
2.9k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.2k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
Test your architecture with Archunit
thirion
2
2.5k
The untapped power of vector embeddings
frankvandijk
2
2k
Automating Front-end Workflow
addyosmani
1370
210k
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? • 間違いとかあればご指摘ください。