PHP if/else if 简写 - 基础

发布于 2024-10-22 15:36:44 字数 475 浏览 10 评论 0原文

我有一段现有代码,但我无法理解。

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

当我遇到这个时:

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

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

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

我认为这非常简单,但在实践中我得到了不同的结果。上面两个片段有什么区别?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

熟人话多 2024-10-29 15:36:44

它们绝对相同,差异一定在其他地方。

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

不过,我同意 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;
}

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;
}
奢欲 2024-10-29 15:36:44

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

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

<?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

They should be identical. I'll make a test file, but I don't think it will bring change to that fact...

Woah, made a testfile:

<?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";

and the results where indeed the same.

Type A
Type B
Type C
Type A
Type B
Type C
东风软 2024-10-29 15:36:44

$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;
}

这样你就可以随时看到不需要的数据是什么,具体取决于我总是设置的情况默认,特别是在开发模式下。

is $type returning an undesirable? if it is, I would :

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

But I'm in total agreement with the above guys, actually you should be translating it as:

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;
}

That way you can always see what the undesirable data will be, depending on the circumstances I'd always set a default, esp in dev mode.

七堇年 2024-10-29 15:36:44

这实际上不是 简写 语法。这只是一个 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';

This isn't actually shorthand syntax. This is just an if/else if/else if where each section only has one statement and thus doesn't need a {} set.

It's a little clearer when formatted with line breaks:

if($type == 'a')
    $type = 'Type A';
else if($type == 'b')
    $type = 'Type B';
else if($type == 'c')
    $type = 'Type C';
岁月如刀 2024-10-29 15:36:44

我认为你首先需要 php switch case 来简化上面的代码。

尽管我必须提到,我没有发现您的代码的两个版本有任何代码差异。确实,switch case 使其比许多 if、else if、else if 语句更具可读性。

I think you first need php switch case to simplify above code.

Although I must mention that I didn't find any code differences 2 versions of your code. It is jsut that switch case makes it more readable than many if, else if, else if, statements.

花开浅夏 2024-10-29 15:36:44

它们是相同的,错误在其他地方。

they are identical, the error is elsewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文