2
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 5 years have passed since last update.

【PHP】PHP単体でforeachelseが使いたいとき

Posted at

使わなくていいから(良心)











Smartyとか使うとforeachの対象のリストが空だったときなんかに通る処理が{foreachelse}~{/foreach}に書けるけど
あるいはPythonなんかだとfor~else~なんかで実現できるんですけど
PHPでやろうとすると

if ($arrData) {
    foreach ($arrData as $k => $v) {
        // うーんかっこわるい
    }
} else {

}

なんとなくきちゃない。
どうしても使いたいときは、こう書くとすこしだけきれいに収まる。

if (!$arrData) {
    // こっちがelse的な内容だから順序がね・・・
} else foreach ($arrData as $k => $v) {
    // あんま使わないほうがいいかも
}
2
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
2
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?