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
-
1Please provide the contents of $str and $array. :)Teekin– Teekin2010年12月09日 21:04:02 +00:00Commented Dec 9, 2010 at 21:04
-
1I agree stick a var_dump($str); var_dump($array) at the start of the function.GWW– GWW2010年12月09日 21:04:56 +00:00Commented Dec 9, 2010 at 21:04
-
1have you debugged using print_r on your array?wajiw– wajiw2010年12月09日 21:05:25 +00:00Commented Dec 9, 2010 at 21:05
-
1Show the exact code that invokes the function.mario– mario2010年12月09日 21:05:42 +00:00Commented Dec 9, 2010 at 21:05
-
1There 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– Jonah2010年12月09日 21:12:35 +00:00Commented Dec 9, 2010 at 21:12
3 Answers 3
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 Comment
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 Comment
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