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条)

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题