Jump to content

Recommended Posts

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.

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])
}
}

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';
}

 

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.