简体   繁体   English

(如果(x == y)返回true,否则返回false与(如果(x!= y)返回false,否则返回true)有什么区别?

[英]What is the difference between (if (x == y) return true, else false) and (if (x != y) return false, else true)?

if($send != true)
    {
        echo "Error Sending Email: ". $send->message;
        return false;
    }
    else 
    {
        echo "Mail Sent";
        return true;
    }

I'm learning PHP right now, and am struggling to understand why this code is set up the way it is. 我现在正在学习PHP,并且正在努力理解为什么以这种方式设置此代码。 Is there any difference between the above code and this: 上面的代码和这个之间有什么区别:

if($send == true)
    {
        echo "Mail Sent";
        return true;
    }
    else 
    {
        echo "Error Sending Email: ". $send->message;
        return false;
    }

It seems to me that they would do the same thing, and I can't think of any reasons why to use one over the other. 在我看来,他们会做同样的事情,而且我想不出为什么要在另一个上使用一个的原因。 Is it just preference, or am I missing something? 只是偏爱,还是我错过了什么? I'm a beginner so any input would be greatly appreciated. 我是一个初学者,因此任何输入将不胜感激。

There is no difference and mostly you can do whatever you like. 没有区别,大多数情况下您可以做任何喜欢的事情。

Please keep in mind that you do not need to compare boolean values with true and false cause they are already okay for the condition. 请记住,您不需要将布尔值truefalse进行比较,因为它们已经可以满足条件了。 For example 例如

if ($send) {
    echo "Mail Sent";
    return true;
} else {
    echo "Error Sending Email: ". $send->message;
    return false;
}

You can also return the $send value like this 您也可以像这样返回$send

if ($send) {
    echo "Mail Sent";
} else {
    echo "Error Sending Email: ". $send->message;
}
return $send;

When you choose what order to use I suggest to think about which one is easier to read and understand. 当您选择使用哪种顺序时,我建议考虑哪种顺序更易于阅读和理解。

ps One of the important topics in PHP is Type Juggling PHP automatically converts different types to whatever is needed. ps PHP中的一个重要主题是类型杂耍 PHP会自动将不同的类型转换为所需的任何类型。 Just read this and keep in mind when you do different conditional checks. 只需阅读此内容,并在进行不同的条件检查时记住。 Sometimes this might be tricky. 有时这可能很棘手。

pps There is a great book my Martin Fowler - Refactoring: Improving the Design of Existing Code It covers questions like this - how to organize the code, what is the best way to do conditional checks etc. Well, this is not the only book about the subject but this was the first one I thought about. pps我的马丁·福勒(Martin Fowler)有一本很棒的书-重构:改进现有代码的设计它涵盖了这样的问题-如何组织代码,进行条件检查的最佳方法是什么等。嗯,这不是唯一的书主题,但这是我想到的第一个主题。 It might be arguable and not all people follow his recommendation but this is something you might want to know :) 这可能是有争议的,并不是所有人都遵循他的建议,但这是您可能想知道的事情:)

I prefer to do my error validation first, so that I don't have to wrap everything following my error validation into its own separate conditional. 我更喜欢先进行错误验证,这样我就不必将错误验证后的所有内容包装到其自己的独立条件中。

Consider the following: 考虑以下:

if(!$send) {
    echo "Error Sending Email: ". $send->message;
    return false;
}
echo "Mail Sent";
return true;

For me this is just easier to read, and becomes much easier to manage when dealing with multiple validation criteria. 对我来说,这更容易阅读,并且在处理多个验证条件时也变得更容易管理。 Just throw all your validation criteria in at the beginning and you're gold (assuming that, when you have an error, you exit the function). 刚开始时就将所有验证条件都扔进去了,那么您就大吃一惊(假设发生错误时,您退出函数)。 Once it passes every validation check, it just chugs right along. 一旦通过每一个验证检查,它就顺手推开。

The main take away, however, is that there is no "right" answer. 然而,主要的收获是没有“正确”的答案。 It's strictly a matter of preference. 严格来说,这是一个优先事项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM