PHP - 如果是这种情况,什么都不做

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3634381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:30:33  来源:igfitidea点击:

PHP - If something is the case, do nothing

phplogic

提问by MEM

Is this a proper way to say: if something is the case, do nothing?

这是正确的说法吗:如果是这样,那就什么都不做?

if ( ($hostNameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput) )
{
  return;
}

Update:I'm not inside a function. :( So the return is just nonsense.

更新:我不在函数内。:( 所以回报只是无稽之谈。

Here is more code:

这是更多代码:

//if the input fields are equal to database values, no need to update and waste resources,hence, do nothing:
if ( ($hostNameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput) )
{
  //do nothing
}
//If, however, (they are NOT equal and) input fields are not empty:
elseif (!empty($hostNameInput) && (!empty($hostAddressInput)))
{
 //do something.
}

Thanks in advance, MEM

提前致谢,MEM

回答by Serty Oan

Maybe you should do the opposite, do something if your condition is not verified

也许你应该做相反的事情,如果你的条件没有得到验证就做点什么

if($hostNameInfo != $hostNameInput || $hostAddressInfo != $hostAddressInput) {
   // do something
}

回答by Roberto

For do nothing you simply can type:

对于什么都不做,您只需键入:

function relax() {
    ;
}

if (($hostNameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput)) {
    relax();
}

回答by jensgram

I assume you're inside a function in which case it does what you expect, although multiple returnstatements within a function canlead to confusion and a lack of readability.(Apparently I was wrong.)

我假设您在一个函数中,在这种情况下它会执行您期望的操作,尽管return函数中的多个语句可能会导致混乱和缺乏可读性。(显然我错了。)

Instead, I prefer to let all conditional blocks (my description for the code between in the if's {...}block) contain the relevant code, i.e., write the conditional check in such a way that the total condition evaluates to truewhen additional processing (sub-flow) is needed:

相反,我更愿意让所有条件块(我对if's{...}块之间的代码的描述)包含相关代码,即,以总条件评估为true附加处理(子流)的方式编写条件检查) 需要:

if ($hostNameInfo != $hostNameInput || $hostAddressInfo != $hostAddressInput) {
    // do stuff, else skip
}

Furthermore, you can extract the conditional statement in order to improve bothreadability and simplicity of control flow:

此外,还可以为了提高提取条件语句可读性和控制流的简单:

$hostInfoEqualsInput = ($hostNameInfo == $hostNameInput && $hostAddressInfo == $hostAddressInput);
if (!$hostInfoEqualsInput) {
    ...
}


UPDATE(based on updated question). Consider this instead:

更新(基于更新的问题)。考虑一下:

$fieldsAreFilled = (!empty($hostNameInput) && !empty($hostAddressInput));
$hostInfoEqualsInput = ($hostNameInfo == $hostNameInput && $hostAddressInfo == $hostAddressInput);

if ($fieldsAreFilled && !$hostInfoEqualsInput) {
    ...
}


ERGO
Minimize branch rate and avoid empty blocks by writing conditions you wantto be met, not all the exceptions you want to ignore (subjective).

ERGO
通过写入您想要满足的条件来最小化分支率并避免空块,而不是您想要忽略的所有异常(主观)。

回答by Andrejs Cainikovs

You're talking about best practices here..
One of best practice things is that routine shall have single exit point, though it is widelydiscussedand is up to developer/style.

您在这里谈论的是最佳实践。
最佳实践之一是例程应具有单一出口点,尽管它被广泛讨论并且取决于开发人员/风格。

UPDATE:

更新:

New answer, since the question was changed:

新答案,因为问题已更改:

Don't see any reason to add additional checks if the code should run only under some circustances. To make the code more readable, you should stuck to whatever you accept as easy-maintainable, like this (or something similar):

如果代码只应在某些情况下运行,则没有任何理由添加额外的检查。为了使代码更具可读性,您应该坚持使用您认为易于维护的任何内容,如下所示(或类似的东西):

// Do something only if required
if (($hostNameInfo != $hostNameInput) || ($hostAddressInfo != $hostAddressInput)) &&
    !empty($hostNameInput) && !empty($hostAddressInput))
{
    echo 'place some code here';
}

回答by Viktor Stískala

The other possibility is to throw a new exception, which you can later catch in your application.

另一种可能性是抛出一个新的异常,您可以稍后在应用程序中捕获该异常。

UPDATE:not inside the function this is probably a bad idea.

更新:不在函数内部,这可能是个坏主意。