LoginSignup
1
0

More than 5 years have passed since last update.

PHP で "false" は true になるから注意

Posted at

PHP で、文字列の "false" は true になるので注意しましょう。

$test = "false";
var_dump((bool) $test);

// => bool(true)

環境変数に格納した値を評価したりする場合は要注意です。

// デバッグモードを環境変数に保存
putenv("debug_mode=false");

if (getenv("debug_mode")) {
    echo "debug_mode = ON";
} else {
    echo "debug_mode = OFF";
}

// => 予想に反して debug_mode = ON が出力されてしまう

=== を使用して評価するか、 0 or 1 を使用すると良いでしょう。

// デバッグモードを環境変数に保存
putenv("debug_mode=0");

if (getenv("debug_mode")) {
    echo "debug_mode = ON";
} else {
    echo "debug_mode = OFF";
}

// => 想定通り debug_mode = OFF が出力される
1
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
1
0