开发者

Why will this function not work?

I have the following function that I know should return TRUE but it will not.

function myFunc($str,$array)
{
  foreach($array as $k=>$v) 
  {
    if(strtolower($v) == strtolower($str)) 
    { 
      return TRUE; 
    }
  }
  return false;
}

This function is used inside a class in an if statement if($this->myFunc($something, $array)){

No matter what I do, it will not return true even though I echo some text above the return TRUE; and that is displayed. Any help for something I am missing, that would be great.

Sorry for not posting the codes.

My array prints the following

Array
(
[0] => -1
[1] => Platinum
[2] => 169
)

and

$something = '-1';

I am trying to return true if -1 exists. The problem I don't think is if the value i开发者_如何学Gos in the array. The issue I have is to why it will not return as true, it will echo a value but it will not return anything. I tried using in_array and the function still did not return as true, which is why I tried this method. Could this be an issue with my PHP version? I used strtolower because this function will be reused throughout the page to search for other values.

Thanks


I don't see no nothing wrong with your function, but as alternative you could try:

return in_array(strtolower($str), array_map("strtolower", $array));


This function is used inside a class in an if statement if($this->myFunc($something, $array))
Does modifying the if statement to the following change the answer?

if(myFunc($something, $array))

Just a thought as perhaps $this isn't being evaluated at the correct time.


You can use in_array or strcmp to accomplish this.

Return Values

Returns < 0 if str1 is less than str2;

0 if str1 is greater than str2, and 0 if they are equal

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜