dsaba Posted October 23, 2007 Share Posted October 23, 2007 <?php function findGreaterThanInArr($arr, $num) { //returns 1st key in arr if the value is > than num foreach ($arr as $k => $number) { if ($number > $num) { $foundNum = $k; break; } } if (isset($foundNum)) { return $foundNum; } else { return FALSE; } } function freakLoop() { $startArr = array(0, 1, 2, 3, 4, 5); $endArr = array(5,8,3,9,3); foreach ($startArr as $key => $start) { $endKey = findGreaterThanInArr($endArr, $start); //doesnt stop looping //$endKey = 'hello'; //this one does echo "the key is: $key<br>"; if ($endKey != FALSE) { die(); } } } //run the thing: freakLoop(); ?> Ok see the above code, see the difference in the loop when you uncomment/comment these two lines: $endKey = findGreaterThanInArr($endArr, $start); //doesnt stop looping $endKey = 'hello'; //this one does in both cases $endKey will not != FALSE , but in the case of calling the function, it continues looping ..very weird behavior, how do I get it to not do this?? Whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/74481-solved-freak-loop-acts-differently-on-using-nested-functions-why/ Share on other sites More sharing options...
Daniel0 Posted October 23, 2007 Share Posted October 23, 2007 If you do $endKey = 'hello'; then $endKey != FALSE will evaluate to true as $endKey is true. Quote Link to comment https://forums.phpfreaks.com/topic/74481-solved-freak-loop-acts-differently-on-using-nested-functions-why/#findComment-376372 Share on other sites More sharing options...
dsaba Posted October 23, 2007 Author Share Posted October 23, 2007 yes it enters the if statement when endKey = 'hello' and then it dies... (nothing is wrong with this) the problem is that when call the function findGreaterThanInArr() it also should evaluate to != FALSE, yet it does not enter the if statement and does not die() why does it do this? Quote Link to comment https://forums.phpfreaks.com/topic/74481-solved-freak-loop-acts-differently-on-using-nested-functions-why/#findComment-376376 Share on other sites More sharing options...
dsaba Posted October 23, 2007 Author Share Posted October 23, 2007 yes it enters the if statement when endKey = 'hello' and then it dies... (nothing is wrong with this) why? because exactly what you said 'hello' is evaluated to != FALSE the problem is that when I call the function findGreaterThanInArr() it also should evaluate to != FALSE, yet it does not enter the if statement and does not die() why does it do this? the endArr once run through the findGreaterThanInArr() function should always return an integer number or in other words != FALSE so as far as we're concerned shouldn't the if($endKey != FALSE) treat the endKey variable the SAME, whether its hello or an integer value from the function Quote Link to comment https://forums.phpfreaks.com/topic/74481-solved-freak-loop-acts-differently-on-using-nested-functions-why/#findComment-376390 Share on other sites More sharing options...
dsaba Posted October 23, 2007 Author Share Posted October 23, 2007 figured it out.. and you didn't daniel!! the problem is: in the first iteration findGreaterThanInArr() does indeed return an integer value, and this integer value is the first key of the $endArr , which is of course 0 0 just also happens to evaluate to Boolean False, and this is why the statement fails if the first element of the $endArr had had a different key other than 0, it would have worked Solved! Quote Link to comment https://forums.phpfreaks.com/topic/74481-solved-freak-loop-acts-differently-on-using-nested-functions-why/#findComment-376419 Share on other sites More sharing options...
Daniel0 Posted October 23, 2007 Share Posted October 23, 2007 If yous still want to return int 0, then you could check if it's !== FALSE. That should work. Quote Link to comment https://forums.phpfreaks.com/topic/74481-solved-freak-loop-acts-differently-on-using-nested-functions-why/#findComment-376517 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.