RETURN TRUE 在 php 函数中做什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2636737/
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 07:10:25  来源:igfitidea点击:

What does RETURN TRUE do in a php function?

phpcakephp

提问by Imran

I was just looking at this code and i don't understand what RETURN TRUE does or what the point of it is? Can someone please explain?

我只是在看这段代码,我不明白 RETURN TRUE 的作用或它的意义是什么?有人可以解释一下吗?

class Elephpant {

    public $colour;

    public function dance() {
        echo "elephpant dances!\n";
        return true;
    }
}

Thankyou in advance ;-)

先感谢您 ;-)

采纳答案by Gordon

It returns the boolean TRUE to whatever called dance(). That's all.

它将布尔值 TRUE 返回到任何称为 dance() 的地方。就这样。

You would have to look at the consuming code to see if it makes something from it.

您将不得不查看使用代码以查看它是否从中产生了一些东西。

回答by Kyle Rozendo

In that specific piece of code - not very much.

在那段特定的代码中 - 不是很多。

In general however it would be used to return a condition of a validation or code that needs to return either a positive or a negative.

然而,通常它会用于返回需要返回正值或负值的验证或代码的条件。

For instance, one would do the following:

例如,您会执行以下操作:

public function isValidEmail($email) {
    // do work to see if email is valid
    if(/* Condition making it true */)
       return true;
    else
       return false;
}

回答by Sirber

because it's TRUE, elephpant does dance ;)

因为它是TRUE,大象会跳舞;)

回答by Kemo

Logically, returns boolean TRUE, but in this case doesn't make any sense since it'll return TRUE anyways.

从逻辑上讲,返回布尔值 TRUE,但在这种情况下没有任何意义,因为它无论如何都会返回 TRUE。

回答by Jasper

Sometimes a method/function returns a boolean value to indicate if the operation was succesfull. In the given example it always returns "TRUE".

有时,方法/函数会返回一个布尔值来指示操作是否成功。在给定的示例中,它始终返回“TRUE”。

The calling code can then act upon succesfull completion of the code

然后调用代码可以在代码成功完成后采取行动

if(dance()) echo "succes" else echo "fails"

if(dance()) echo "succes" else echo "fails"

回答by Kamil Szot

You can read more about returnhere: http://www.php.net/return

您可以return在此处阅读更多信息:http: //www.php.net/return

There are few interesting applications of returnlike returning value from include-d file.

很少有有趣的应用程序return像从include-d 文件返回值。