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

PHP if文の条件式にnullが来るとfalseとして振る舞われるのを知らなかった

Posted at

目的

  • 先輩方のソースを見ていて意外と知らなかったif文の条件式にnullを入れたときの振る舞いについて簡単にまとめる

情報

紹介

  1. 下記のような簡単なPHPのソースが合ったとする。

    <?php
    
    $str = 'aaa';
    
    if ($str) {
        echo '変数はnullではありません';
    } else {
        echo '変数はnullです';
    }
    
  2. 上記を実行すると「変数はnullではありません」と出力される。

  3. $strにnullを格納してみる。

    <?php
    
    $str = null;
    
    if ($str) {
        echo '変数はnullではありません';
    } else {
        echo '変数はnullです';
    }
    
  4. 上記を実行すると「変数はnullです」と出力される。

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