bradg
bradg

Reputation: 723

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 is 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

Upvotes: 1

Views: 109

Answers (3)

Amir Raminfar
Amir Raminfar

Reputation: 34149

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

Upvotes: 0

Woot4Moo
Woot4Moo

Reputation: 24316

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.

Upvotes: 1

mario
mario

Reputation: 145482

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));

Upvotes: 6

Related Questions