1
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.

PHP 「true」「false」などの文字列をbooleanのtrue falseに変換する

Posted at

概要

  • 文字列の「true」や「false」をboolean型のtrueやfalseに変換する方法をまとめる。

方法

  • filter_var()関数を使う。下記の様に記載する。下記のように記載することでfilter_varからboolが返される。

    filter_var(「true」や「false」などの文字列, FILTER_VALIDATE_BOOLEAN)
    
  • 下記に具体的な例を記載する。

    <?php
    
    $boolStr = 'true';
    var_dump($boolStr); // string(4) "true"
    
    $bool = filter_var($boolStr, FILTER_VALIDATE_BOOLEAN);
    
    var_dump($bool); // bool(true)
    

注意点

  • FILTER_VALIDATE_BOOLEANフィルターはデフォルト状態で"1", "true", "on", "yes"の場合trueを返し、それ以外の場合はfalseを返す。

参考文献

1
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
1
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?