---
title: PHPのnull + nullはなぜ0になるのか。PHPにおけるnullの扱いを理解して仲良くなろう
tags:  #php #null  
author: [Kazuki Tamahori](https://www.docswell.com/user/tyamahori)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/9J2GP9YVJR.jpg?width=480
description: PHPカンファレンス沖縄 2023の登壇資料です。
published: September 16, 23
canonical: https://www.docswell.com/s/tyamahori/KGXWQN-php-null
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/9J2GP9YVJR.jpg)

PHPのnull + nullはなぜ0にな
るのか。PHPにおけるnullの扱
いを理解して仲良くなろう
PHPカンファレンス 沖縄 2023年9月16日
tyamahori (ちゃまほり)/玉堀和輝


# Page. 2

![Page Image](https://bcdn.docswell.com/page/DEYV54GQEM.jpg)

もくじ
• 自己紹介
• 登壇背景
• nullとはなにか
• null + nullの挙動
• PHP以外
• PHP
• php-srcを読む
• PHPの世界でのnullとの付き合い方
• まとめ


# Page. 3

![Page Image](https://bcdn.docswell.com/page/VJNMNYG278.jpg)

自己紹介


# Page. 4

![Page Image](https://bcdn.docswell.com/page/YE9NRPWDE3.jpg)

tyamahori(ちゃまほり)
本名
玉堀和輝(たまほり かずき)
X(旧:Twitter) @tyamahori
生年月日
1989/09/24
出身/在住
千葉県香取市/千葉県船橋市
経歴
Webアプリケーション開発者(PHP/Laravel)
2017/05 ~
所属
株式会社LITALICO 2022/08 ~
キーワード
設計/DDD/クリーンアーキテクチャ
/Laravel/PHP/Docker/OrbStack/Jetbrains/P
hpStorm/環境構築


# Page. 5

![Page Image](https://bcdn.docswell.com/page/GE8KWDN57D.jpg)

WEB+DB PRESS Vol.135 で執筆しました
https://amazon.co.jp/dp/429713571X/


# Page. 6

![Page Image](https://bcdn.docswell.com/page/LELZNMP3JR.jpg)

【宣伝】


# Page. 7

![Page Image](https://bcdn.docswell.com/page/4JMVXY4MJW.jpg)

株式会社LITALICO
読み: りたりこ 由来: 利他(りた)と利己(りこ)
• ビジョン「障害のない社会をつくる」
• 事業内容: 障害者福祉サービスに関するもの
• エンジニア募集中
• 「技術の力で社会解決」したい方！
• ぜひともお話しましょう! 詳細はこちら
• HP: https://litalico.co.jp/


# Page. 8

![Page Image](https://bcdn.docswell.com/page/PJR2N9WLE9.jpg)

登壇背景


# Page. 9

![Page Image](https://bcdn.docswell.com/page/PEXDNQV67X.jpg)

仕様の理解不足
• null + null = 0 の挙動を理解していなかった
• バグ対応に時間を溶かし、悔しい思いをした
• どういう挙動なのかが気になった


# Page. 10

![Page Image](https://bcdn.docswell.com/page/3EKXN9QG7D.jpg)

そもそもnullってなんだ？


# Page. 11

![Page Image](https://bcdn.docswell.com/page/L735VWP5E5.jpg)

ネット上での定義


# Page. 12

![Page Image](https://bcdn.docswell.com/page/87DD8KLY7G.jpg)

トイレットペーパーの例
0より大きい
null
0
null


# Page. 13

![Page Image](https://bcdn.docswell.com/page/VJPX8K2278.jpg)

つまり、nullとは？
• 「なにもない」や「存在しない」
• 0ではない
• 空文字ではない


# Page. 14

![Page Image](https://bcdn.docswell.com/page/2EVLNVDXJQ.jpg)

他言語のnull + nullの挙動は？


# Page. 15

![Page Image](https://bcdn.docswell.com/page/57GXKLYREL.jpg)

Java


# Page. 16

![Page Image](https://bcdn.docswell.com/page/4EQ8NYMYJP.jpg)

Ruby


# Page. 17

![Page Image](https://bcdn.docswell.com/page/KJ42GWVZE1.jpg)

Python


# Page. 18

![Page Image](https://bcdn.docswell.com/page/LE1MDY3D7G.jpg)

TypeScript


# Page. 19

![Page Image](https://bcdn.docswell.com/page/GEW3YGP8E2.jpg)

JavaScript


# Page. 20

![Page Image](https://bcdn.docswell.com/page/47ZVXLK9E3.jpg)

Go


# Page. 21

![Page Image](https://bcdn.docswell.com/page/YJ654WQD7V.jpg)

Perl


# Page. 22

![Page Image](https://bcdn.docswell.com/page/GJ5YQMR8J4.jpg)

比較結果
• null + null はエラーになることが多い
• PHPとしてnullの演算はどう扱うのがただしいのかは論点にはなりそう
• PHP,JavaScript,Perlの場合は0
Java
Error
Ruby
Error
Python
Error
TypeScript
Error
JavaScript
0
Go
Error
Perl
0


# Page. 23

![Page Image](https://bcdn.docswell.com/page/9E2GP9DVER.jpg)

PHPのnullを使った四則演算


# Page. 24

![Page Image](https://bcdn.docswell.com/page/D7YV542Q7M.jpg)

検証するPHPのバージョン


# Page. 25

![Page Image](https://bcdn.docswell.com/page/VENMNY82J8.jpg)

四則演算(足算、引算、掛算)


# Page. 26

![Page Image](https://bcdn.docswell.com/page/Y79NRPKD73.jpg)

四則演算(割算)


# Page. 27

![Page Image](https://bcdn.docswell.com/page/G78KWDR5JD.jpg)

nullを利用した比較演算、キャスト


# Page. 28

![Page Image](https://bcdn.docswell.com/page/L7LZNMK3ER.jpg)

PHPの挙動まとめ
• 四則演算ではnullは0になる
• null !== 0
• null !== ‘’
• (int)null === 0


# Page. 29

![Page Image](https://bcdn.docswell.com/page/4EMVXY2MEW.jpg)

php-srcでnull + nullの挙動をみる


# Page. 30

![Page Image](https://bcdn.docswell.com/page/PER2N9ML79.jpg)

php-src環境
リポジトリURL
https://github.com/php/php-src
ブランチ
https://github.com/php/php-src/tree/PHP-8.2.8
PHP version
PHP 8.2.8-dev (cli) (built: Aug 20 2023 23:11:51) (NTS DEBUG)


# Page. 31

![Page Image](https://bcdn.docswell.com/page/P7XDNQK6JX.jpg)

大まかな流れ
処理の起動
スクリプトの解析し、構造体を生成
構造体をもとにOpcode生成
Opcode実行
• php-srcを読んでみよう @tzm_freedomさん
• PHPの関数実行とその計測（記事版） - Qiita @sji_chさん


# Page. 32

![Page Image](https://bcdn.docswell.com/page/37KXN95GJD.jpg)

null + null が0に置き換わる
https://github.com/php/php-src/blob/PHP-8.2.8/Zend/zend_operators.c#L275


# Page. 33

![Page Image](https://bcdn.docswell.com/page/LJ35VWK575.jpg)

null + null;
• nullの構造体とそのコピーがポインタ引数で渡る。
• 第一引数をもとに、型のチェックをし、nullの場合、コピーである第2
引数のデータ構造に対して、0を割り当てる
• 後続処理で、0を割り当てたholderを利用する。


# Page. 34

![Page Image](https://bcdn.docswell.com/page/8JDD8K3YJG.jpg)

array_sum([null])におけるnullの変換
https://github.com/php/php-src/blob/PHP-8.2.8/Zend/zend_operators.c#L192


# Page. 35

![Page Image](https://bcdn.docswell.com/page/VEPX8K42J8.jpg)

まとめ


# Page. 36

![Page Image](https://bcdn.docswell.com/page/27VLNVXXEQ.jpg)

PHPの null + null = 0の由来はどこから？
• 歴史的経緯は不明
• メーリングリストやGitHubのissueにはそれらしい情報はに当たらず。。。
• PHPは当時は型が今ほど固くはなかったからか
• 暗黙的にnull + nullは0に変換して処理を行うことが当時の思想にあっていた
説？
• Perl, awkコマンドの影響を受けた説？


# Page. 37

![Page Image](https://bcdn.docswell.com/page/5JGXKLVR7L.jpg)

null+nullが0の問題点
• 暗黙的な型変換により、意図しな
い処理結果になりうる
• nullはどう扱う？
• 本当にnull+nullは0で良いのか？
• DBや外部APIからデータを持ってき
たときnullがあったらどうする？
• countするときにnullの数は？含
める含めない？
• 平均値を出すときの分母変わる


# Page. 38

![Page Image](https://bcdn.docswell.com/page/47Q8NY6YEP.jpg)

null + null = 0 と付き合う
• 数値を扱う場面でnullがある場合を考慮する
• nullは外す、もしくは無視する
• 何かしらの数値に置き換える
• 例外を投げて処理をやめる
• nullがどう変換されるかは、関係者で決める
• 共通理解のもと、業務ロジックが組み立てられる。
• 型の相互変換(Type Juggling)の仕様は要チェックや！
• https://www.php.net/manual/ja/language.types.type-juggling.php
• 整数への変換ルール
• `null は、常に0に変換されます。`との記載はある
• https://www.php.net/manual/ja/language.types.integer.php#language.type
s.integer.casting


# Page. 39

![Page Image](https://bcdn.docswell.com/page/KE42GW4Z71.jpg)

あるべき姿について
• nullのキャスト周りはエラーになるべき
• 暗黙的な変換が行われるとバグの温床
• null と 0 は違う。


# Page. 40

![Page Image](https://bcdn.docswell.com/page/L71MDY4DJG.jpg)

おわりに
• PHPでは、null + nullは0です！
• 仕様です。
• この挙動を理解した上で、PHPと付き合
っていこう！
• nullのハンドリングはどうするかは、
関係者で共通認識を持とう！
https://twitter.com/tyamahori/status/1685507147889782784


# Page. 41

![Page Image](https://bcdn.docswell.com/page/G7W3YGX872.jpg)

ご清聴ありがとうございました


# Page. 42

![Page Image](https://bcdn.docswell.com/page/4JZVXL6973.jpg)

参考資料
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
https://github.com/php/php-src
https://github.com/nickdiego/compiledb
https://speakerdeck.com/tzmfreedom/php-src-codereading
https://3v4l.org/
https://dev.to/ramsey/using-clion-with-php-src-4me0
https://www.jetbrains.com/ja-jp/clion/
https://www.php.net/manual/ja/language.types.null.php
https://blog.freedom-man.com/2022
https://blog.hanhans.net/2021/07/06/php-step-debug-clion/
https://www.slideshare.net/do_aki/phpsrc
https://zenn.dev/hgsgtk/articles/fb9af8bfc64ec37bce2d
https://wa3.i-3-i.info/word13709.html
https://amazon.co.jp/dp/429713571X/
PHP の関数実行とその計測（記事版） - Qiita
https://tech.respect-pal.jp/php-helloworld
https://www.php.net/manual/en/language.types.type-juggling.php


