1

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

9
  • 1
    Please provide the contents of $str and $array. :)
    – Teekin
    Dec 9, 2010 at 21:04
  • 1
    I agree stick a var_dump($str); var_dump($array) at the start of the function.
    – GWW
    Dec 9, 2010 at 21:04
  • 1
    have you debugged using print_r on your array?
    – wajiw
    Dec 9, 2010 at 21:05
  • 1
    Show the exact code that invokes the function.
    – mario
    Dec 9, 2010 at 21:05
  • 1
    There is nothing wrong with the function you've given here. Either this is not the exact code you are using, it really shouldn't return true, or there's a problem with your usage of it. Please tell us how you're using it.
    – Jonah
    Dec 9, 2010 at 21:12

3 Answers 3

6

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));
1
  • +1, Essentially the same function, but uses two loops instead.
    – Jonah
    Dec 9, 2010 at 21:11
1

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.

1
  • If I remove $this, it throws an error that the function is undefined.
    – bradg
    Dec 9, 2010 at 21:26
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.