如何正确格式化 PHP“IF ELSE” 声明?

发布于 2024-07-14 09:59:10 字数 926 浏览 5 评论 0原文

这是一个长期存在的问题,我在许多热门的编码会议中都遇到过。

一个人这样编码,另一个人那样编码。 所以经过一番推拉之后我很好奇...... 有没有正确的方式来表达 PHP 'IF ELSE' 语句?

就我个人而言,我使用:

if ($variable == 'setvalue')
{
    $variable = executefunctiononvariable($variable);
} else {
    $variable = executedifferentfunctiononvariable($variable);
}

尽管经过多次争论,我已经看到了其他选项,例如:

if ($variable == 'setvalue')
{
    $variable = executefunctiononvariable($variable);
}
else
{
    $variable = executedifferentfunctiononvariable($variable);
}

if ($variable == 'setvalue')
    $variable = executefunctiononvariable($variable);
else
    $variable = executedifferentfunctiononvariable($variable);

if ($variable == 'setvalue') {
    $variable = executefunctiononvariable($variable);
} else {
    $variable = executedifferentfunctiononvariable($variable);
}

It's been a long running issue that I've come across in many-a-hot-and-steamy coding sessions.

One person codes this way another codes that way. So after much push and pull I'm curious...
Is there any correct way of phrasing a PHP 'IF ELSE' statement?

Personally I use the:

if ($variable == 'setvalue')
{
    $variable = executefunctiononvariable($variable);
} else {
    $variable = executedifferentfunctiononvariable($variable);
}

After many arguments though I've been presented with other options such as:

if ($variable == 'setvalue')
{
    $variable = executefunctiononvariable($variable);
}
else
{
    $variable = executedifferentfunctiononvariable($variable);
}

OR

if ($variable == 'setvalue')
    $variable = executefunctiononvariable($variable);
else
    $variable = executedifferentfunctiononvariable($variable);

OR

if ($variable == 'setvalue') {
    $variable = executefunctiononvariable($variable);
} else {
    $variable = executedifferentfunctiononvariable($variable);
}

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

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

发布评论

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

评论(12

以酷 2024-07-21 09:59:12

我以前一直这样做(2),但由于 Sun 的编码约定使用(4),所以我在 Java 编程中被打败了。 所以现在我已经很习惯(4)了。 我最近做了一些 C# 工作,似乎默认使用 (2)(叹气,我们又来了)。

在 PHP 中,出于习惯,我使用 (4),但 (2) 也可以。 我根本不喜欢(1)

(3) 是危险的。 就我个人而言,我认为语言的语法应该需要大括号,即使它只是一个语句。 免得你陷入麻烦。 我认为 Perl 凭记忆就是这样做的。

我还讨厌的是当人们这样做时:

if (something) {
  // do something
}
else if (something else) {
}

那会让我抓狂。 所以我只觉得(2)和(4)可以接受。 我不在乎它是哪一个,只要它一致地完成,最好是在该语言的约定范围内。

I used to do (2) all the time but got it beaten out of me from Java programming as Sun's coding conventions use (4). So now I'm pretty used to (4). I've been doing a bit of C# lately and it seems to use (2) by default (sigh, here we go again).

In PHP from habit I do (4) but (2) is fine too. I don't like (1) at all.

And (3) is dangerous. Personally I think braces should be required by the syntax of the langauge even if its just for one statement. Saves you getting into trouble. I think that's how Perl does it from memory.

What I also hate is when people do this:

if (something) {
  // do something
}
else if (something else) {
}

That one drives me batty. So I only find (2) and (4) acceptable. I don't care which one it is, as long as it's done consistently, preferably within the conventions for the language.

幸福丶如此 2024-07-21 09:59:12

方法没有对错之分,只是一种意见。 就我个人而言,我最喜欢最后一个(1TBS???)。 我从不使用不带牙套的那种,我认为它总体上是一种不好的风格。

唯一能够真正为您回答这个问题的人是其他将要编写代码的人。 每个人都同意编码标准很重要。 您选择哪种标准并不重要,重要的是每个人都使用它。

There is no right or wrong way, it is an opinion. Personally, I like the last one best (1TBS???). I never use the one without braces, I consider it bad style in general.

The only people that can really answer this question for you are the other people that are going to work on the code. It is important that everone agrees to a coding standard. Which standard you choose is less important than the fact that everyone uses it.

梦醒灬来后我 2024-07-21 09:59:12

PEAR 编码标准是 PHP 编码标准。 我建议您习惯它,因为您会在其他项目中找到它,例如 Zend、Doctrine、Symfony、Horde 等等。

http://framework.zend.com/manual/en/coding-standard.coding-style.html#coding-standard.coding-style.control-statements.if-else-elseif

The PEAR coding standard is the PHP coding standard. I would recommend to get used to it as you will find it in other projects such as Zend, Doctrine, Symfony, Horde and many, many more.

http://framework.zend.com/manual/en/coding-standard.coding-style.html#coding-standard.coding-style.control-statements.if-else-elseif

寄居者 2024-07-21 09:59:12

简而言之,这不是正确的做法。 只要能用,你觉得最好的都可以用。 你应该选择一个然后坚持下去,它会让你的代码更容易被识别。

唯一的问题是,如果不包含“{”字符,则只能使用一个表达式或函数。

另外,如果您只想定义变量,可以使用以下代码:

$variable = (CONDITIONAL STATEMENT) ? "It was true" : "It was false"; 

In short, the is no correct way of doing. As long as it works, whatever you feel is the best, you can use. You should pick one and then stick to it, it will make your code easier to recognise.

The only thing is, if you don't include the "{" character you are limited to one expression or function.

Also, if you are only looking to define variables you can use the following code:

$variable = (CONDITIONAL STATEMENT) ? "It was true" : "It was false"; 
那一片橙海, 2024-07-21 09:59:12

在我的公司,我们使用:

if ($variable == 'setvalue')
{
    $variable = executefunctiononvariable($variable);
}
else
{
    $variable = executedifferentfunctiononvariable($variable);
}

只要有标准,我并不重要

At my company we use:

if ($variable == 'setvalue')
{
    $variable = executefunctiononvariable($variable);
}
else
{
    $variable = executedifferentfunctiononvariable($variable);
}

I doesn't really matter aslong as there is a standard

枕头说它不想醒 2024-07-21 09:59:12

真的对我来说……这并不重要。 我相信您应该能够毫无问题地阅读任何一种方式。 花括号是否在新行上真的很重要吗? 右括号后面是否有空格真的很重要吗?

只要代码以至少尝试使其可读的方式完成,我真的不在乎。

有正确的方法吗? 好吧,如果有的话,那么为什么我们可以选择以不同的方式做呢?

Really to me... it just doesn't matter. I believe you should be able to read either way without issues. Does it really matter if the curly brace is on a new line or not? Does it really matter if there's a space after the closing parenthesis or not?

As long as the code is done in a such way that there's been at least an attempt at making it readable, I really just don't care.

Is there a correct way? Well if there was, then why do we have options of doing it differently?

调妓 2024-07-21 09:59:11

我个人将 if/else 的格式设置为最后一个:

if ($variable == 'setvalue') {
    $variable = executefunctiononvariable($variable);
} else {
    $variable = executedifferentfunctiononvariable($variable);
}

在我看来,您的版本是 1 和 3 的混合体。

我也曾与完成所有这些工作的编码员一起工作,但从未听说过标准编码员。

php网站使用最后一种:https://www.php。 net/manual/en/control-structs.elseif.php

在某些情况下,当 if 语句总是很短时,我也会使用第二个示例。 如果有可能它变得更长(每行超过 1 行),我会做#1。 我尽量避免使用#2,因为以后很难添加 {}。

I personally format my if/else like the last one:

if ($variable == 'setvalue') {
    $variable = executefunctiononvariable($variable);
} else {
    $variable = executedifferentfunctiononvariable($variable);
}

Your version is kind a mixture of 1 and 3, in my mind.

I have also worked with coders that do all of them and have never heard of a standard one.

The php website uses the last one: https://www.php.net/manual/en/control-structures.elseif.php

I also use the second example in some cases when the if statement will always be very short. If there's ever a possibiltiy of it getting longer (more than 1 line each) I'll do #1. I try to avoid #2 when possible cause it's hard to add the {} later.

無處可尋 2024-07-21 09:59:11

我使用最后一种:

if ($variable == 'setvalue') {
    $variable = executefunctiononvariable($variable);
} else {
    $variable = executedifferentfunctiononvariable($variable);
}    

话虽如此,选择哪一种并不重要,只要确保一致即可。

I use the last one:

if ($variable == 'setvalue') {
    $variable = executefunctiononvariable($variable);
} else {
    $variable = executedifferentfunctiononvariable($variable);
}    

That being said, it is pretty unimportant which one you go with, just make sure you are consistent.

能否归途做我良人 2024-07-21 09:59:11

正确的方法是遵循项目的编码标准。 如果您没有,请采用 PHP-FIG、Zend、Symfony 等中的一种。

这种形式似乎非常流行:

if (condition) {
    statements
} else {
    statements
}

对于变量赋值,如果语句可以清晰地适应,我将使用三元一行:

$variable = !empty($foo) ? $foo : 'default';

更新:我删除了有关多行三元语句的部分,因为我不再认为这是一种明智的做法。

The Right Way is to follow your project's coding standard. If you don't have one, adopt one from PHP-FIG, Zend, Symfony, etc.

This form appears very popular:

if (condition) {
    statements
} else {
    statements
}

For variable assignment I'll use a ternary only if the statement can fit legibly on one line:

$variable = !empty($foo) ? $foo : 'default';

Update: I've removed the bit about a multi-line ternary statements as I no longer consider this a wise practice.

万劫不复 2024-07-21 09:59:11

我个人更喜欢:

if(something){
    doSomething();
}
elseif(somethingElse){
    doSomethingElse();
}
else{
    doAnotherThing();
}

I personnally prefer:

if(something){
    doSomething();
}
elseif(somethingElse){
    doSomethingElse();
}
else{
    doAnotherThing();
}
苏佲洛 2024-07-21 09:59:11

不要忘记我个人喜欢这种结构。

if (expression):
   // code goes here
elseif (another expression):
   // code goes here
else:
   // code goes here
endif;

当我煮标签汤时,

Don't forget about

if (expression):
   // code goes here
elseif (another expression):
   // code goes here
else:
   // code goes here
endif;

I personally like this structure when I'm cooking some tag soup.

檐上三寸雪 2024-07-21 09:59:11

最重要的是,从事项目的程序员几乎都遵守相同编码准则。 因此,召开一次会议并选择其中之一,然后坚持下去。

The most important thing is that the programmers working on a project pretty much adhere to the same coding guidelines. So have a meeting and pick one or the other, and then stick with it.

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