TheTitans Posted November 21, 2008 Share Posted November 21, 2008 I have an array that is set up like this: Array ( [0] => 0 [1] => Happiness [2] => Stone ) Now I have my function: public function calcEvolutionMethod($value) { if ($value == 0) { return "Basic Form"; } else { return $value; } } Each element in the array is used in the function. "Basic Form" is returned for the first result, which is good; however "Basic Form" is returned for the second and third results too, which is not suppose to happen. Instead, for the second and third result, the else statement needs to kick in and just return "Happiness" and "Stone". I'm not sure what I'm doing wrong. Quote Link to comment https://forums.phpfreaks.com/topic/133602-array-not-passing-into-function-correctly/ Share on other sites More sharing options...
genericnumber1 Posted November 21, 2008 Share Posted November 21, 2008 Can you show us the code that is calling this function? Quote Link to comment https://forums.phpfreaks.com/topic/133602-array-not-passing-into-function-correctly/#findComment-695015 Share on other sites More sharing options...
TheTitans Posted November 21, 2008 Author Share Posted November 21, 2008 Yes. for ($i = 0; $i < 1; $i++) { $y2 = explode(",", $x2[$i]); // $y2 is the array that I posted above. The elements 0, Happiness, and Stone are in this array. for ($j = 0; $j < 3; $j++) { // 3 elements in the array; we loop 3 times and display the results. "Basic Form" gets displayed three times, when it should only be displayed once echo $dex->calcEvolutionMethod($y2[$j]) } } Quote Link to comment https://forums.phpfreaks.com/topic/133602-array-not-passing-into-function-correctly/#findComment-695045 Share on other sites More sharing options...
Mark Baker Posted November 21, 2008 Share Posted November 21, 2008 Have you looked at what happens when you compare a string with a number: $string = 'Happiness'; if ($string == 0) { echo 'Match'; } else { echo 'No Match'; } $string = 'Happiness 4 all'; if ($string == 4) { echo 'Match'; } else { echo 'No Match'; } $string = 'Happiness'; if ($string === 0) { echo 'Match'; } else { echo 'No Match'; } Quote Link to comment https://forums.phpfreaks.com/topic/133602-array-not-passing-into-function-correctly/#findComment-695201 Share on other sites More sharing options...
Adam Posted November 21, 2008 Share Posted November 21, 2008 Can tidy your code up a lot- by the way using foreach () to loop through an array is much easier! I don't quite know what you were trying to do with the first for (), perhaps just loop once as there's only one entry in the array? Remember a loop is for repeating code, could have just used: $y2 = explode(",", $x2[0]);.. But I've changed the second for () as an example to show you.. for ($i = 0; $i < 1; $i++) { // $y2 is the array that I posted above. The elements 0, Happiness, and Stone are in this array. $y2 = explode(",", $x2[$i]); // 3 elements in the array; we loop 3 times and display the results. "Basic Form" gets displayed three times, when it should only be displayed once foreach ($y2 as $val) { echo $dex->calcEvolutionMethod($val); } } Try changing this line to: echo $val . ': ' . $dex->calcEvolutionMethod($val); So you know it's definitely getting the right values passed.. Adam Quote Link to comment https://forums.phpfreaks.com/topic/133602-array-not-passing-into-function-correctly/#findComment-695212 Share on other sites More sharing options...
TheTitans Posted November 21, 2008 Author Share Posted November 21, 2008 Found the error; weird error too. if ($value == 0) {} ...That had to be written as: if ($value == "0") {} MrAdam: I know about the foreach statement. The reason why I didn't use it is because there's other code in there. I didn't include it because it was basic HTML and SQL queries that weren't necessary to show. Quote Link to comment https://forums.phpfreaks.com/topic/133602-array-not-passing-into-function-correctly/#findComment-695277 Share on other sites More sharing options...
Mark Baker Posted November 21, 2008 Share Posted November 21, 2008 Found the error; weird error too.Not weird at all. If you'll look back to my initial post, it's exactly what I said, and is absolute standard behaviour for PHP Quote Link to comment https://forums.phpfreaks.com/topic/133602-array-not-passing-into-function-correctly/#findComment-695337 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.