0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PHP】0はnull?

Posted at

はじめに

超初歩的なところなんでしょうけど
知らなかったので反省をこめ、書いていきます。

変数==0

以下のような条件式を書いてました。

.php
$test = null;

if($test == 0){
  echo $test."はZEROです。(実際はNULL)<br>";
}else{
  echo "ZEROじゃない";
}
.結果
はZEROです。(実際はNULL)

なお、falseでもそうなるそうです。

正しい条件式

単純に===にして
型も比較すればよいのです。
反省します。

.php
$test = null;

if($test === 0){
  echo $test."はZEROです。(実際はNULL)<br>";
}else{
  echo "ZEROじゃない(思い通り)";
}
.結果
ZEROじゃない(思い通り)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?