doutanggun9816 2011-03-17 21:01
浏览 232
已采纳

PHP简写if / else if - Basic

I've got a piece of existing code that I'm having problems understanding.

I generally don't like shorthand because it requires config changes, and is harder for me to read. For this reason I'm not particularly familiar with it. The existing code was written by someone who loves shorthand.

When I encountered this:

if($type == 'a') $type = 'Type A';
else if($type == 'b') $type = 'Type B';
else if($type == 'c') $type = 'Type C';

I read it as a simple if, and else if string. I converted it to:

if($type == 'a') {
  $type = 'Type A';
} else if($type == 'b') {
  $type = 'Type B';
} else if($type == 'c') {
  $type = 'Type C';
}

I thought that was pretty straightforward, however I'm getting different results in practice. What's the difference between the two snippets above?

  • 写回答

6条回答 默认 最新

  • douyan8772 2011-03-17 21:03
    关注

    They're absolutely identical, the difference must be elsewhere.

    Is that a copy/paste of the before and after code?

    I agree with anubhava though, I'd tend to convert that to a switch case for clarity:

    switch ($type) {
      case 'a':
        $type = 'Type A';
        break;
    
      case 'b':
        $type = 'Type B';
        break;
    
      case 'c':
        $type = 'Type C';
        break;
    
      default:
        break;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效