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
マジカルインクリメントと指数表記
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
tsuda.a
December 08, 2024
Programming
0
230
マジカルインクリメントと指数表記
PHP の特徴的な機能の一つである文字列へのインクリメントに関する注意喚起と、指数表記という普段あまり目にしないリテラル表記についての紹介です。
tsuda.a
December 08, 2024
Tweet
Share
More Decks by tsuda.a
See All by tsuda.a
バックアップしていますか?
tsudaahr
0
140
RDB以前のファイル設計の話でもしようか(ぇ
tsudaahr
0
140
NPUわからん
tsudaahr
0
200
計算量オーダーの話
tsudaahr
1
430
クラウド初学者が抱える不安について
tsudaahr
0
310
キューとは何か
tsudaahr
0
260
等幅は死んだ(ぇ
tsudaahr
0
120
いくら眺めてもエラーの理由がわからないコードについて
tsudaahr
0
210
何のために文字数をカウントするのか?
tsudaahr
0
120
Other Decks in Programming
See All in Programming
RailsのValidatesをSwift Macrosで再現してみた
hokuron
0
130
PHP でエミュレータを自作して Ubuntu を動かそう
m3m0r7
PRO
2
140
KagglerがMixSeekを触ってみた
morim
0
280
OTP を自動で入力する裏技
megabitsenmzq
0
130
Fundamentals of Software Engineering In the Age of AI
therealdanvega
2
290
Migration to Signals, Signal Forms, Resource API, and NgRx Signal Store @Angular Days 03/2026 Munich
manfredsteyer
PRO
0
150
Codex CLIのSubagentsによる並列API実装 / Parallel API Implementation with Codex CLI Subagents
takatty
2
420
The free-lunch guide to idea circularity
hollycummins
0
350
Nostalgia Meets Technology: Super Mario with TypeScript
manfredsteyer
PRO
0
110
「効かない!」依存性注入(DI)を活用したAPI Platformのエラーハンドリング奮闘記
mkmk884
0
180
Pythonデータ分析コトハジメinFukuoka
kanan
0
100
ロボットのための工場に灯りは要らない
watany
12
3.2k
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.2k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
410
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.8k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
980
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.2k
Thoughts on Productivity
jonyablonski
75
5.1k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
490
Git: the NoSQL Database
bkeepers
PRO
432
67k
Tell your own story through comics
letsgokoyo
1
870
Mobile First: as difficult as doing things right
swwweet
225
10k
Docker and Python
trallard
47
3.8k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
230
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? • 間違いとかあればご指摘ください。