简体   繁体   English

为什么此函数总是返回false?

[英]Why this function always returning false?

I have a very basic question. 我有一个非常基本的问题。 It's may very poor question but I just want to clear my confusion. 这可能是一个很糟糕的问题,但我只是想消除我的困惑。

function check_array($user_value,$array)
    {
        foreach ($array as $key => $value) {
            if($value==$user_value)
            {
                return true;
            }
            return false;
        }
        //return false;
    }

Why this function always returning false. 为什么此函数总是返回false。

for example If I have $numbers = array(1,2,3) . 例如,如果我有$numbers = array(1,2,3) If I match 2 with this array it should return me true else return me false. 如果我将此数组与2匹配,则应该返回true,否则返回false。 But why it's returning always false ? 但是为什么它总是返回false?

The return false is in the wrong place, it should be where you commented it out but not in the other place it is. return false在错误的位置,应该是在您注释掉它的位置,而不是在其他位置。

function check_array($user_value,$array)
    {
        foreach ($array as $key => $value) {
            if($value==$user_value)
            {
                return true;
            }
        }
        return false;
    }

If the return is in the other place, it will return false after failing to find your value in the first array cell. 如果返回在另一个位置,则在第一个数组单元中找不到您的值后,它将返回false。

why it's returning always false ? 为什么它总是返回false?

Try to "execute" this function yourself acting as a computer. 尝试自己充当计算机来“执行”此功能。

If the first element of array is equal to $user_value , it will return true. 如果array的第一个元素等于$user_value ,它将返回true。 If not - it will move futher down the loop and return false. 如果不是,它将进一步在循环中移动并返回false。

Probably, you wanted to check all the elements of array for equality. 可能您想检查array的所有元素是否相等。 In this case you need to use this: 在这种情况下,您需要使用以下命令:

function check_array($user_value,$array)
    {
        foreach ($array as $key => $value) {
            if($value==$user_value)
            {
                return true;
            }
        }
        return false;
    }

You dont need to create a function for it when php has already have it in_array 当PHP已经拥有in_array时,您无需为其创建函数

you can use it like in a single line 您可以像单行一样使用它

$result = in_array($user_value,$array)

It will return true if the value is found and false if not found. 如果找到该值,它将返回true;如果找不到,则返回false。 You need not to loop through the array 您无需遍历数组

You have the return false inside of the loop. 您在循环内部有return false。 That means if the first element isn't a match, it will return false. 这意味着,如果第一个元素不匹配,它将返回false。 It won't even check the second element of your array 它甚至不会检查数组的第二个元素

Your function will be returning false, unless it matches the first value in the array. 您的函数将返回false,除非它与数组中的第一个值匹配。

This is because at the moment your logic is in the wrong order, and if the first value in the array doesn't match $user_value the function returns false. 这是因为当前您的逻辑顺序错误,并且如果数组中的第一个值与$ user_value不匹配,则该函数返回false。

You could solve this problem by moving the return false; 您可以通过将return false;来解决此问题return false; line of code below the loop. 循环下面的代码行。 Now the loop will run through all the values in the array. 现在,循环将遍历数组中的所有值。 It will return true if it matches one, and if it has checked all the values and there is no match then it will then return false. 如果匹配一个,它将返回true;如果已经检查了所有值,但没有匹配,则它将返回false。

function check_array($user_value,$array)
    {
        foreach ($array as $key => $value) {
            if($value==$user_value) return true;
        }
        return false;
    }

This should clear up the logic mistake in your code. 这应该清除代码中的逻辑错误。 However I up-voted Veerendra's in_array() answer as that means there is no need for any loop, cutting down your code. 但是我投票赞成Veerendra的in_array()答案,因为这意味着不需要任何循环,从而减少了代码。

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

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