LoginSignup
1

More than 1 year has passed since last update.

【PHP】エクスクラメーションマーク2つでbooleanに型変換する

Posted at

PHPのコードを読んでいて、以下のような書き方を見かけた。

$count = $this->user->where('name', $name)->count();
return !!$count;

$countには0もしくは1の数字が入っている。

この$countに対して「!」の否定演算子を2回連続で行うと、boolean型に変換できるらしい。

つまり、以下の2つは同じ意味になる。

return (boolean)$count;
return !!$count;

初めて見た書き方だったので、覚書き。

参考記事

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