【问题标题】:PHP Shorthand if/else if - BasicPHP 速记 if/else if - 基本
【发布时间】:2011-07-17 17:47:57
【问题描述】:

我有一段现有代码在理解上有问题。

我通常不喜欢速记,因为它需要更改配置,而且我更难阅读。由于这个原因,我不是特别熟悉它。现有代码是由喜欢速记的人编写的。

当我遇到这个时:

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

我把它理解为一个简单的 if 和 else if 字符串。我将其转换为:

if($type == 'a') { $type = 'A 型'; } 否则 if($type == 'b') { $type = 'B 型'; } 否则 if($type == 'c') { $type = 'C 型'; }

我认为这很简单,但是在实践中我得到了不同的结果。上面的两个sn-ps有什么区别?

【问题讨论】:

  • 你得到了什么结果?
  • 两个代码sn-ps是一样的。
  • 顺便说一句,如果您上面的代码很好地代表了您的实际代码,您可能需要查看 switch 语句
  • 我将 $type 设置为不正确的值,谢谢大家的帮助,我在其他地方一定有错误。

标签: php shorthand


【解决方案1】:

它们完全一样,区别肯定在别处。

这是前后代码的复制/粘贴吗?

虽然我同意 anubhava,但为了清楚起见,我倾向于将其转换为 switch case:

switch ($type) {
  case 'a':
    $type = 'Type A';
    break;

  case 'b':
    $type = 'Type B';
    break;

  case 'c':
    $type = 'Type C';
    break;

  default:
    break;
}

【讨论】:

  • 感谢您的澄清和建议,我同意我应该将其更改为开关。
【解决方案2】:

它们应该是相同的。我会制作一个测试文件,但我认为它不会改变这个事实......

哇,制作了一个测试文件:

<?php
$type = 'a';

if($type == 'a') $type = 'Type A';
else if($type == 'b') $type = 'Type B';
else if($type == 'c') $type = 'Type C';
echo $type . "\n";


$type = 'b';

if($type == 'a') $type = 'Type A';
else if($type == 'b') $type = 'Type B';
else if($type == 'c') $type = 'Type C';
echo $type . "\n";


$type = 'c';

if($type == 'a') $type = 'Type A';
else if($type == 'b') $type = 'Type B';
else if($type == 'c') $type = 'Type C';
echo $type . "\n";


$type = 'a';

if($type == 'a') {
  $type = 'Type A';
} else if($type == 'b') {
  $type = 'Type B';
} else if($type == 'c') {
  $type = 'Type C';
}
echo $type . "\n";

$type = 'b';

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

echo $type . "\n";
$type = 'c';

if($type == 'a') {
  $type = 'Type A';
} else if($type == 'b') {
  $type = 'Type B';
} else if($type == 'c') {
  $type = 'Type C';
}
echo $type . "\n";

结果确实一样。

Type A
Type B
Type C
Type A
Type B
Type C

【讨论】:

  • 我希望在编辑之前没有人看到;)。代码是一样的:)
  • 太好了,谢谢!我一定是在其他地方出现错误导致我的奇怪结果。
【解决方案3】:

$type 是否返回了不受欢迎的值?如果是,我会:

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

但我完全同意上面的人,实际上你应该把它翻译成:

switch ($type) {
  case 'a':
    $type = 'Type A';
    break;

  case 'b':
    $type = 'Type B';
    break;

  case 'c':
    $type = 'Type C';
    break;

  default:
    $type = 'Other Type';
    break;
}

这样你总是可以看到不需要的数据是什么,这取决于我总是设置默认值的情况,尤其是在开发模式下。

【讨论】:

    【解决方案4】:

    这实际上不是shorthand 语法。这只是一个 if/else if/else if,其中每个部分只有一个语句,因此不需要 {} 集。

    用换行符格式化会更清晰一些:

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

    【讨论】:

    • 感谢您的澄清。
    【解决方案5】:

    我认为您首先需要php switch case 来简化上述代码。

    虽然我必须提到我没有发现任何代码差异 2 版本的代码。只是 switch case 比许多 if、else if、else if 语句更具可读性。

    【讨论】:

    • 感谢您的推荐。
    【解决方案6】:

    它们是相同的,错误在别处。

    【讨论】:

      猜你喜欢
      • 2014-08-08
      • 2016-10-06
      • 2016-12-26
      • 2016-05-15
      • 2013-04-08
      • 2013-07-18
      • 1970-01-01
      • 2013-08-10
      • 2014-09-07
      相关资源
      最近更新 更多