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

asked Dec 9, 2010 at 21:02
9
  • 1
    Please provide the contents of $str and $array. :) Commented Dec 9, 2010 at 21:04
  • 1
    I agree stick a var_dump($str); var_dump($array) at the start of the function. Commented Dec 9, 2010 at 21:04
  • 1
    have you debugged using print_r on your array? Commented Dec 9, 2010 at 21:05
  • 1
    Show the exact code that invokes the function. Commented 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. Commented 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));
answered Dec 9, 2010 at 21:07
Sign up to request clarification or add additional context in comments.

1 Comment

+1, Essentially the same function, but uses two loops instead.
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.

answered Dec 9, 2010 at 21:12

1 Comment

If I remove $this, it throws an error that the function is undefined.
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

answered Dec 9, 2010 at 21:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.