LoginSignup
5
5

More than 5 years have passed since last update.

phpのboolをintにキャストする際

Posted at

ちょっとメモ

bool型をint型にキャストして値を取得したいがために以下の要に記述
しかしこのままで帰ってくるのはbool型

/**
 * @return int
 */
public function getPayType()
{
    return (int) $this->getCoinFlag() === self::COIN_FLAG;
}

何故かっていったら左方から優先して型変換を行ってくれたので
上記のような記述方法だと以下のように解釈される

public function getPayType()
{
    return ((int) $this->getCoinFlag()) === self::COIN_FLAG;
}

実際に帰ってくるのはbool型

なので以下のように変更して上げる事で目的のint型が帰ってきてくれる

/**
 * @return int
 */
public function getPayType()
{
    return (int) ($this->getCoinFlag() === self::COIN_FLAG);
}
5
5
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
5
5