6
6

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は整数の0と空文字列を==で比較するとtrueになる。

Last updated at Posted at 2014-09-27

PHPは整数の0と空文字列を==で比較するとtrueになる。

test.php
<?php

$num = 0;

var_dump($num);

$str = '';

var_dump($str);

var_dump($num == $str);

var_dump($num != $str);

var_dump($num === $str);

var_dump($num !== $str);

var_dump(intval($str));
?>

結果は以下の通り

int(0)
string(0) ""
bool(true)
bool(false)
bool(false)
bool(true)
int(0)

空文字列はintにcastすると0になる。

6
6
2

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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?