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?

【empty()関数】0がnull扱いになる事にハマった

Last updated at Posted at 2024-01-03

きっかけ

以下のようなステータスのプルダウンで検索処理を実装した時にハマった

有効 無効
0 1

状況

プルダウンでりんごを選択した時に検索出来ない

<select name="fruit">
    <option value = "0">りんご</option>
    <option value = "1">れもん</option>
    <option value = "2">メロン</option>
</select>
<input type="submit"name="submit"value="検索"/>

原因

empty()関数で0はnull扱いだった
りんごを選択した時、この条件式ではfalseになるため検索がかからなかった

if(!empty($all["fruit"])){
}

解決策

ひとまず以下の条件式で動いたが、issetを使うとか、valueの値に0を使わないほうがよかったかも。

if($all["fruit"] != ""){
}
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?