Wrong value after going through if clauses

I am writing a script to compare two different voting systems. The following code is part of a very long DO loop in my code. When I run the script, the first time through the DO loop, it prints out the final sentence with the wrong value in the variable $remove_candidate. The program prints out “Doing 1.” and “Doing 2.”, showing that the first two if clauses are being activated, but not the ones after those. Therefore the value contained in the variable $remove_candidate should be the value contained in $candidate2_name, which, in the case I am running is, “Dem”. However, the final sentence prints out “SuperDem”, which is the value contained in $candidate1_name. (This is verified by the code that prints out the values contained in these variables.) Does anyone know why it would print out the value of $candidate1_name, rather than $candidate2_name?

if ($candidate1_irv_vote < $remove_number and $candidate1_out == 'No')
	{	$remove_number = $candidate1_irv_vote;
		$remove_candidate = $candidate1_name;
################# temporary ####################
		echo "<p>Doing 1.</p>"; // Shouldn't do this 2nd time through
#########################################################
	}
if ($candidate2_irv_vote < $remove_number and $candidate2_out == 'No') 
	{	$remove_number = $candidate2_irv_vote;
		$remove_candidate = $candidate2_name;
################# temporary ####################
		echo "<p>Doing 2.</p>";
#########################################################
	}
if ($candidate3_irv_vote < $remove_number and $candidate3_out == 'No')
	{	$remove_number = $candidate3_irv_vote;
		$remove_candidate = $candidate3_name;
################# temporary ####################
		echo "<p>Doing 3.</p>";
#########################################################
	}
if ($candidate4_irv_vote < $remove_number and $candidate4_out == 'No') 
	{	$remove_number = $candidate4_irv_vote;
		$remove_candidate = $candidate4_name;
################# temporary ####################
		echo "<p>Doing 4.</p>";
#########################################################
	}
if ($candidate5_irv_vote < $remove_number and $candidate5_out == 'No') 
	{	$remove_number = $candidate5_irv_vote;
		$remove_candidate = $candidate5_name;
################# temporary ####################
		echo "<p>Doing 5.</p>";
#########################################################
	}
if ($candidate6_irv_vote < $remove_number and $candidate6_out == 'No') 
	{	$remove_number = $candidate6_irv_vote;
		$remove_candidate = $candidate6_name;
################# temporary ####################
		echo "<p>Doing 6.</p>";
#########################################################
	}
if ($candidate7_irv_vote < $remove_number and $candidate7_out == 'No') 
	{	$remove_number = $candidate7_irv_vote;
		$remove_candidate = $candidate7_name;
################# temporary ####################
		echo "<p>Doing 7.</p>";
#########################################################
	}
if ($candidate8_irv_vote < $remove_number and $candidate8_out == 'No') 
	{	$remove_number = $candidate8_irv_vote;
		$remove_candidate = $candidate8_name;
################# temporary ####################
		echo "<p>Doing 8.</p>";
#########################################################
	}
if ($candidate9_irv_vote < $remove_number and $candidate9_out == 'No') 
	{	$remove_number = $candidate9_irv_vote;
		$remove_candidate = $candidate9_name;
################# temporary ####################
		echo "<p>Doing 9.</p>";
#########################################################
	}
if ($candidate10_irv_vote < $remove_number and $candidate10_out == 'No') 
	{	$remove_number = $candidate10_irv_vote;
		$remove_candidate = $candidate10_name;
################# temporary ####################
		echo "<p>Doing 10.</p>";
#########################################################
	}
############## temporary #################
echo "<p>candidate1_name: $candidate1_name</p>";
echo "<p>candidate2_name: $candidate2_name</p>";
echo "<p>candidate3_name: $candidate3_name</p>";
echo "<p>candidate4_name: $candidate4_name</p>";
echo "<p>candidate5_name: $candidate5_name</p>";
echo "<p>candidate6_name: $candidate6_name</p>";
echo "<p>candidate7_name: $candidate7_name</p>";
echo "<p>candidate8_name: $candidate8_name</p>";
echo "<p>candidate9_name: $candidate9_name</p>";
echo "<p>candidate10_name: $candidate10_name</p>";
######################################################

// Keep track of candidates that have been removed/voted out
if($remove_candidate = $candidate1_name)
	{	$candidate1_out = "Yes";
####### temporary ################################
		echo "<p>CANDIDATE1_OUT IS NOW YES</P>";
#################################################
	}
elseif($remove_candidate == $candidate2_name)
	{	$candidate2_out = "Yes";}
elseif($remove_candidate == $candidate3_name)
	{	$candidate3_out = "Yes";}
elseif($remove_candidate == $candidate4_name)
	{	$candidate4_out = "Yes";}
elseif($remove_candidate == $candidate5_name)
	{	$candidate5_out = "Yes";}
elseif($remove_candidate == $candidate6_name)
	{	$candidate6_out = "Yes";}
elseif($remove_candidate == $candidate7_name)
	{	$candidate7_out = "Yes";}
elseif($remove_candidate == $candidate8_name)
	{	$candidate8_out = "Yes";}
elseif($remove_candidate == $candidate9_name)
	{	$candidate9_out = "Yes";}
elseif($remove_candidate == $candidate10_name)
	{	$candidate10_out = "Yes";}

echo "<p>The candidate removed after round $round_number of voting is $remove_candidate.</p>";

Really gotta learn to use arrays… it’ll save you SO much code space…

if($remove_candidate = $candidate1_name)

Your problem is in this line. Can you spot it? Hint: There’s 1 of something where there should be 2 (or potentially even 3!).

Yes. I got the double = in all the rest of the lines. How did I miss it in that one? I fixed it, and at least that piece of the program is working. Thank you for your sharp eye.

And you’re probably right about arrays. I was sure there was a much simpler and cleaner way to do this than all the thousands of lines of code that I have written, but I wasn’t quite sure what it was.

Just about any time you find yourself doing something like $var1, $var2, $var3, …, the answer is to use an array instead. That lets you easily loop over the data to perform your common code operations rather than copy and paste the code a bunch of times and change the numbers.

I’d recommend using boolean true/false instead of ‘Yes’ / ‘No’ also. That’s a little more of a personal style choice though.

Your code using an array would be something like this:

### Example showing the array format
$candidateList = [
    ['irv_vote' => 1, 'out' => false, 'name' => 'a'], //[0]
    ['irv_vote' => 10, 'out' => false, 'name' => 'b'], //.
    ['irv_vote' => 20, 'out' => false, 'name' => 'c'], //.
    ['irv_vote' => 30, 'out' => false, 'name' => 'd'], //.
    ['irv_vote' => 40, 'out' => false, 'name' => 'e'], //.
    ['irv_vote' => 50, 'out' => false, 'name' => 'f'], //.
    ['irv_vote' => 60, 'out' => false, 'name' => 'g'], //.
    ['irv_vote' => 70, 'out' => false, 'name' => 'h'], //.
    ['irv_vote' => 80, 'out' => false, 'name' => 'i'], //.
    ['irv_vote' => 90, 'out' => false, 'name' => 'j'], //[9]
];
############

$remove_index = null;
foreach ($candidateList as $index => $candidate){
    if ($candidate['irv_vote'] < $remove_number and !$candidate['out']){
        $remove_number = $candidate['irv_vote'];
        $remove_candidate = $candidate['name'];
        $remove_index = $index; //Track the index number of the candidate to be removed.
    }
}

if ($remove_index !== null){
    $candidateList[$remove_index]['out'] = true;
    echo "<p>The candidate removed after round $round_number of voting is $remove_candidate.</p>";
}

Yes, I need to get a better handle on arrays. Thank you for the example.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.