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
290
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
130
バックアップしていますか?
tsudaahr
0
170
RDB以前のファイル設計の話でもしようか(ぇ
tsudaahr
0
180
NPUわからん
tsudaahr
0
230
計算量オーダーの話
tsudaahr
1
480
クラウド初学者が抱える不安について
tsudaahr
0
470
キューとは何か
tsudaahr
0
310
等幅は死んだ(ぇ
tsudaahr
0
150
いくら眺めてもエラーの理由がわからないコードについて
tsudaahr
0
250
Other Decks in Programming
See All in Programming
Agents on Rails - Rails at Scale 2026
irinanazarova
0
130
Verilogで学ぶCPU自作入門.pdf
uyuki234
1
270
テストを司るデーモンに会いに行く 〜隔離した仮想マシンでテストを通すまで〜
h1d3mun3
1
550
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
260
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
230
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
2
290
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
230
Are APIs Still Relevant in the AI Era?
soyuka
0
290
一人だけ、Kiroが静止する日
hideg
0
120
Family mrubyの進捗
kishima
1
130
FreeBSDでZabbixを動かす
kenkino
0
310
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
490
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Test your architecture with Archunit
thirion
2
2.4k
The Language of Interfaces
destraynor
162
27k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
The Curse of the Amulet
leimatthew05
3
15k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
So, you think you're a good person
axbom
PRO
2
2.2k
Technical Leadership for Architectural Decision Making
baasie
3
580
The untapped power of vector embeddings
frankvandijk
2
1.9k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
200
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
2.9k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
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? • 間違いとかあればご指摘ください。