46
48

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 5 years have passed since last update.

PHPのtrueは1でfalseは''の話

Last updated at Posted at 2015-11-05

タイトルがすべて。

PHPのtrueは1でfalseは''

(すこしとまどう)

<?php

echo 'true:  ' . true  . PHP_EOL;
echo 'false: ' . false . PHP_EOL;
出力
true:  1
false:

'false'という文字列をboolにキャストするとtrue

(あたりまえ)

<?php

$string_true  = 'true';
$string_false = 'false';

echo 'be true ?  : ' . (bool)$string_true  . PHP_EOL;
echo 'be false ? : ' . (bool)$string_false . PHP_EOL;
出力
be true ?  : 1
be false ? : 1

NULL + NULL は 0

<?php

echo 'NULL: ';
echo NULL;
echo PHP_EOL;

echo 'NULL + NULL:';
echo NULL + NULL;
echo PHP_EOL;

echo 'NULL . NULL:';
echo NULL . NULL;
echo PHP_EOL;
出力
NULL:
NULL + NULL:0
NULL . NULL:

備考

'true'や'false'という文字列をboolean型に
<?php

$string_true  = 'true';
$string_false = 'false';

// 'true'や'false'という文字列をboolean型に
echo( ( 'true'  === $string_true  ) ? true  : false ) . PHP_EOL;
echo( ( 'false' === $string_false ) ? false : true  ) . PHP_EOL;
booleanを'true', 'false'という文字列に
<?php

$bool_true  = true;
$bool_false = false;

// booleanを'true', 'false'という文字列に
echo (( true  === $bool_true  ) ? 'true'  : 'false' ) . PHP_EOL;
echo (( false === $bool_false ) ? 'false' : 'true'  ) . PHP_EOL;

おわりです。

参考や注釈

46
48
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
46
48

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?