このエントリーをはてなブックマークに追加
はてなブックマーク - PHP if文: if{}とif: endif;
Share on Facebook
Post to Google Buzz
Bookmark this on Yahoo Bookmark
Bookmark this on Livedoor Clip
Share on FriendFeed
PHP if文: if{}とif: endif;あらかたは回帰、きまぐれは彷徨

phper(phpist?)では無いんですが、最近php書くことが多いので、まあ、逆に言えば、phperでないがゆえのメモ。
まずはよくあるパターンから

一行程度を表示させたい時の条件分岐

<?php if($hoge){
     echo $hage;
  }else{
     echo $huge;
  }
?>

表示させたい時が沢山あるときの条件分岐

<?php if($this->value): ?>
  Hello
<?php elseif($this->asd): ?>
  Your name is: <?= $this->name ?>
<?php else: ?>
  You don't have a name.
<?php endif; ?>

要は、if{}もif: endif;も同じだけど、
echoをたくさん書くのは、MVC使ってるときにやるのいやでしょ?ってことらしいです。

They are the same but the second one is great if you have MVC in your code and don’t want to have a lot of echos in your code. F.e. in my .phtml files (Zend Framework) I will write something like this:

別にif(): endif;じゃなくても、以下のようにすれば、複数行でいけるみたいですが…。
全然美しくない…

<? if($condition) { ?>
   HTML content here
<? } else { ?>
   Other HTML content here
<? } ?>

works cited
http://stackoverflow.com/questions/564130/difference-between-if-and-if-endif