简体   繁体   English

使函数返回0和true

[英]Make a function return both 0 and true

Is this possible? 这可能吗? I see some native php functions can do that. 我看到一些本地php函数可以做到这一点。 For example: strpos() can return 0 which can apparently be true. 例如: strpos()可以返回0,这显然是正确的。

Edit 编辑

When the manual says some function can return both integer 0 and boolean false, it means it can return either integer 0 or boolean false (not both) in any given call. 当手动说,一些功能可以返回两个整数0和布尔假,这意味着它可以返回整数0或布尔false (不同时)在任何给定的呼叫。 PHP is not strictly typed, functions can return different types in different situations. PHP不是严格类型化的,函数可以在不同情况下返回不同的类型。 For instance, the following function returns either 0 or false , depending on whether the passed parameter is non negative or not: 例如,以下函数根据传递的参数是否为非负值而返回0false

function myfunc($arg) {
    if ($arg >= 0)
        return 0;
    else
        return false;
}

Original 原版的

PHP has no multiple return. PHP没有多重回报。 You have two options: 您有两种选择:

Return composite values instead 而是返回复合值

function myfunc() {
    return array(0, true); //return array
}

class MyOutputHolder {
    private $number;
    private $truth;
    function getNumber { return $this->number; }
    function getTruth { return $this->truth; }
    function __construct($number, $truth) {
        $this->number = $number;
        $this->truth = $truth;
    }
}
function myfunc() {
    return new MyOutputHolder(0, true); //return object
}

A third possibility is a custom resource, but that must be implemented internally (in an extension). 第三种可能性是自定义资源,但是必须在内部(扩展中)实现。

Use output parameters 使用输出参数

function myfunc(&$outnumber, &$outtruth) {
    $outnumber = 0;
    $outtruth = true;
}

Of course, you can return only 0 or true and use only one parameter. 当然,您只能返回0true并且只能使用一个参数。

For functions that can return successfully with the return value of zero, you should be using type equivalence checking. 对于可以成功返回零值的函数,您应该使用类型等效检查。

if(somefunction() !== false) {



}

The integer zero is interpreted as false if type is not considered. 如果不考虑类型,则整数零将解释为false。 For example, assuming somefunction returns zero. 例如,假设somefunction返回零。

somefunction() != false

Will be false, while 会是假的,而

somefunction() !== false

Will be true. 会是真的。

Your confusion is that strpos returns the zero-based index of the search string . 您的困惑是strpos返回搜索字符串从零开始的索引

So in this case, 0 is a valid return, it means "found it at index 0" 因此,在这种情况下,0是有效的返回值,表示“在索引0处找到它”

If the string isn't found at all, then it returns FALSE. 如果根本找不到该字符串,则返回FALSE。

It's important to note what's written in big red warning in the strpos doc page: 重要的是要注意strpos doc页面中用大红色警告写的内容:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". 该函数可以返回布尔值FALSE,但也可以返回非布尔值,其值为FALSE,例如0或“”。 Please read the section on Booleans for more information. 请阅读布尔值部分以获取更多信息。 Use the === operator for testing the return value of this function. 使用===运算符测试此函数的返回值。

ie: 0 is not exactly FALSE in php. 即:在PHP中0并不完全是FALSE。 It's fundamental of php. 这是php的基础。 0 == FALSE but 0 !== FALSE 0 ==假但0!==假

As too why PHP can return either a numeric value or a boolean - maybe that's your actual question - PHP isn't strongly typed, you never specify what you'll be returning, so you're free to return different data types depending on the outcome of the function 同样,为什么PHP可以返回数字值或布尔值-也许这是您的实际问题-PHP不是强类型的,所以您无需指定要返回的内容,因此可以自由地返回不同的数据类型,具体取决于功能的结果

Does this count? 这算吗?

function stupid() { 
   return "0\0"; 
}; 

echo stupid() ."\n"; 

var_dump(stupid()); 

if (stupid()) echo "true\n"; 

echo stupid() + 4 . "\n";

Output: 输出:

0
string(2) "0"
true
4

*ducks* *鸭子*

A function can only ever have one return value. 一个函数只能有一个返回值。 However, you can return an array with multiple values if you need to. 但是,如果需要,您可以返回具有多个值的数组。

I think you're misunderstanding what strpos actually returns... 我认为您误会了strpos的实际回报...

strpos() returns either an integer greater than or equal to zero, or it returns false (if the needle character is not found in the string). strpos()返回一个大于或等于零的整数,或者返回false(如果在字符串中未找到针字符)。

0 does not equal true in any sense - what the PHP documentation does mention, though, is that because of PHP's loose-typing, 0 can equal false unless you use the conditional operator that forces type as well as value comparison. 0在任何意义上都不等于true-但是,PHP文档确实提到的是,由于PHP的类型过于宽松,除非您使用强制类型和值比较的条件运算符,否则0可以等于false。

var_dump(0 == false);  // 'true'
var_dump(0 == true);   // 'false'
var_dump(0 === false); // 'false'
var_dump(0 === true);  // 'false'
var_dump(0 !== false); // 'true'

this is why the PHP manual recommends you test the return value from strpos() with '!== false' because the character you are searching for may be the first character in the string and therefore the function returns 0. 这就是PHP手册建议您使用'!== false'测试strpos()的返回值的原因,因为要搜索的字符可能是字符串中的第一个字符,因此该函数返回0。

$string = "_testing";
var_dump(strpos($string, '_'));           // 0
var_dump(strpos($string, '_') !== false); // 'true'
var_dump(strpos($string, '_') === true);  // 'false'
return 0 && true;

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

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