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?

More than 1 year has passed since last update.

boolvalの値とbool値比較評価備忘メモ

Last updated at Posted at 2023-03-07

前提

% php --version
PHP 8.0.18 (cli) (built: Apr 15 2022 09:41:02) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.18, Copyright (c) Zend Technologies
    with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans
    with Zend OPcache v8.0.18, Copyright (c), by Zend Technologies

boolvalをくぐらせてのecho結果

文字列の'false'をboolvalにくぐらせるとtrue判定となる。nullはfalse

echo '0:' . boolval(0)  . PHP_EOL;
echo '1:' . boolval(1) . PHP_EOL;
echo 'true:' . boolval('true') . PHP_EOL;
echo 'false:' . boolval('false') . PHP_EOL;
echo 'true(bool):' . boolval(true) . PHP_EOL;
echo 'false(bool):' . boolval(false) . PHP_EOL;
echo 'mojiretsu:' . boolval('mojiretsu:') . PHP_EOL;
echo 'null:' . boolval(null) . PHP_EOL;

0:
1:1
true:1
false:1
true(bool):1
false(bool):
mojiretsu:1
null:

評価の動作

vs文字列

bool文字列

echo false == 'false'; // 一致しない
echo false === 'false'; // 一致しない

数値文字列 ※==と===で動作が変わるのでややこしい😖

echo false == '0'; // 一致する
echo false === '0'; // 一致しない

vs数値

==と===で動作が変わるのでややこしい😖

echo false == 0; // 一致する
echo false === 0; // 一致しない

boolvalをくぐらせた場合はどちらも一致

echo false == boolval(0); // 一致する
echo false === boolval(0); // 一致する
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?