PHP - 如果出现这种情况,则不执行任何操作

发布于 2024-09-18 01:12:45 字数 658 浏览 3 评论 0原文

“如果出现这种情况,就什么也不做”这样的说法是否正确?

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

更新: 我不在函数内部。 :( 所以返回只是废话。

这是更多代码:

//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.
}

提前致谢, 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

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

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

发布评论

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

评论(6

ゞ花落谁相伴 2024-09-25 01:12:45

如果不执行任何操作,您只需输入:

function relax() {
    ;
}

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

For do nothing you simply can type:

function relax() {
    ;
}

if (($hostNameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput)) {
    relax();
}
┾廆蒐ゝ 2024-09-25 01:12:45

也许你应该做相反的事情,如果你的情况没有得到验证,就做一些事情

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

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

if($hostNameInfo != $hostNameInput || $hostAddressInfo != $hostAddressInput) {
   // do something
}
-黛色若梦 2024-09-25 01:12:45

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

相反,我更喜欢让所有条件块(我对 if{...} 块)包含相关代码,即,在需要额外处理(子流程)时,以总条件计算为 true 的方式编写条件检查:

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

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

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

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

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

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

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

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

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 true when additional processing (sub-flow) is needed:

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

Furthermore, you can extract the conditional statement in order to improve both readability 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 want to be met, not all the exceptions you want to ignore (subjective).

幼儿园老大 2024-09-25 01:12:45

您在这里谈论的是最佳实践..
最佳实践之一是例程应具有 单一退出点,尽管它广泛 已讨论,由开发人员/风格决定。

更新:

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

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

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

You're talking about best practices here..
One of best practice things is that routine shall have single exit point, though it is widely discussed and 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';
}
伏妖词 2024-09-25 01:12:45

有时,本机 do_nothing() 函数会非常好且可读。

为了避免来自语法检查器和语法检查器的压力警报linter,当你有一个空的 if 块时会变得疯狂,我使用:

echo(null);

A native do_nothing() function would be very nice and readable sometimes.

To avoid stressing alerts from syntax checkers & linters, that go crazy when you have an empty if block, I use:

echo(null);
清欢 2024-09-25 01:12:45

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

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

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.

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