Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am not sure why it won't compare?

Status
Not open for further replies.

ppat60

Programmer
Oct 20, 2007
13
0
0
US
I am passing in $data as an array Color, Number(1-6); $RouteTwo is an array of 18 elelments of colors{Red, Yellow, Green, Blue, Black}; and $saveNewPos is a variable that stores a color from another function.

I want to compare $data[0] to $saveNewPos, but it does not appear to work. when I echo the variables separate they me Yellow which is correct, but it does not get into the appropriate elseif statement.

Can someone explain this to me?

Code:
public function ArrayWater($data,$RouteTwo,$saveNewPos)
  {
    echo"(ArrayWater)your in the save new Array function look for which player: <br>";
    echo "data[0] = " .$data[0]. " <br>saveNewPos =  " .$saveNewPos. " <br>";

    if( $data[0] == $saveNewPos){
      $Player_Array[0][2] = "Route Two";
      $Player_Array[0][3] = $RouteTwo[0];
      $Player_Array[0][5] = $RouteTwo;
    }
    elseif($data[0] == $saveNewPos){
    echo "(ARRAYWATER P2)you in array two";
        $Player_Array[1][2] = "Route Two";
        $Player_Array[1][3] = $RouteTwo[0];
        $Player_Array[1][5] = $RouteTwo;
    }
    elseif($data[0] == $saveNewPos){
      $Player_Array[2][2] = "Route Two";
        $Player_Array[2][3] = $RouteTwo[0];
        $Player_Array[2][5] = $RouteTwo;
    }
    elseif($data[0] == $saveNewPos){
      $Player_Array[3][2] = "Route Two";
        $Player_Array[3][3] = $RouteTwo[0];
        $Player_Array[3][5] = $RouteTwo;
    }
    else
      {echo("Error: error in Water Array Function" );}
  }

Thank You in advance.
patrick
 
It's not clear to me what you're trying to do.

<soapbox>
elseif is an evil construct that, as far as I am concerned, should never have been included in PHP. It should never be used, as nested if-else statements and switch blocks are easier to debug.
</soapbox>

Rewriting your code to remove all those elseifs and instead use nested if-else statements, I get the following:

Code:
public function ArrayWater($data,$RouteTwo,$saveNewPos)
{
	echo"(ArrayWater)your in the save new Array function look for which player: <br>";
	echo "data[0] = " .$data[0]. " <br>saveNewPos =  " .$saveNewPos. " <br>";

	if( $data[0] == $saveNewPos)
	{
		$Player_Array[0][2] = "Route Two";
		$Player_Array[0][3] = $RouteTwo[0];
		$Player_Array[0][5] = $RouteTwo;
	}
	else
	{
		if($data[0] == $saveNewPos)
		{
			echo "(ARRAYWATER P2)you in array two";
			$Player_Array[1][2] = "Route Two";
			$Player_Array[1][3] = $RouteTwo[0];
			$Player_Array[1][5] = $RouteTwo;
		}
		else
		{
			if($data[0] == $saveNewPos)
			{
				$Player_Array[2][2] = "Route Two";
				$Player_Array[2][3] = $RouteTwo[0];
				$Player_Array[2][5] = $RouteTwo;
			}
			else
			{
				if($data[0] == $saveNewPos)
				{
					$Player_Array[3][2] = "Route Two";
					$Player_Array[3][3] = $RouteTwo[0];
					$Player_Array[3][5] = $RouteTwo;
				}
				else
				{
					echo("Error: error in Water Array Function" );
				}
			}
		}
	}
}

You're using the same clause in every if-statement, namely [tt]$data[0] == $saveNewPos[/tt] and you're not changing either variable as you go.

Imagine that the statement [tt]$data[0] == $saveNewPos[/tt] evaluates to TRUE. If it does the first block will be run, and once that block is done, the program pointer will end up at the last close-bracket just before the bracket that closes the function.

Now imagine that the statement [tt]$data[0] == $saveNewPos[/tt] evaluates to FALSE. Then no if-statement, all the way down, will allow its block of code to be run. The default behavior, then is the last, innermost else-block that has no if-statment..

As I see it, your code is functionally equivalent to

Code:
public function ArrayWater($data,$RouteTwo,$saveNewPos)
{
	echo"(ArrayWater)your in the save new Array function look for which player: <br>";
	echo "data[0] = " .$data[0]. " <br>saveNewPos =  " .$saveNewPos. " <br>";

	if( $data[0] == $saveNewPos)
	{
		$Player_Array[0][2] = "Route Two";
		$Player_Array[0][3] = $RouteTwo[0];
		$Player_Array[0][5] = $RouteTwo;
	}
	else
	{
		echo("Error: error in Water Array Function" );
	}
}






Want to ask the best questions? Read Eric S. Raymond's essay "How To Ask Questions The Smart Way". TANSTAAFL!
 
sleipnir214

thanks for your insight. after implemented your structure, I am still have the same issue. Which is I can not get any of my if statement to work.

when I echo this out(below)
Code:
echo "data[0] = " .$data[0]. " <br>saveNewPos =  " .$saveNewPos. " <br>";

it returns this: data[0] = Yellow
saveNewPos = Yellow

with that returned above then shouldn't this execute next?
Code:
if($data[0] == $saveNewPos)
        {
            echo "(ARRAYWATER P2)you in array two";
            $Player_Array[1][2] = "Route Two";
            $Player_Array[1][3] = $RouteTwo[0];
            $Player_Array[1][5] = $RouteTwo;

How can I fix this?

Cheers
 
I dunno. Are you sure there are no unseen characters in either string? Try:

Code:
echo "data[0] = '" .$data[0]. "' <br>saveNewPos =  '" .$saveNewPos. "' <br>";




Want to ask the best questions? Read Eric S. Raymond's essay "How To Ask Questions The Smart Way". TANSTAAFL!
 
Okay so I am think I have it narrowed down to what the issue is now, however I do not know how to fix. I am using a for loop to cycle through an arra like this.
Code:
for($i = 0; $i <= $data[1]; $i++)
{
    echo("(MoveArray) landed on : ");
    echo( $saveNewPos = $Array[$i]. "<br>");
}

So then I pass this value $saveNewPos which holds a color let's say Yellow. So now when I try and compare $saveNewPos I can not. How can I make this vaule comparible?
Code:
if($data[0] == $saveNewPos)
where both $data[0] and $saveNewPos equal Yellow.

 
Please verify the values in the variables. With the testing methdology you described earlier, you might be trying to match "Yellow" to "Yellow " (notice the trailing space that would not have been visible) and not detecting it visually.





Want to ask the best questions? Read Eric S. Raymond's essay "How To Ask Questions The Smart Way". TANSTAAFL!
 
sleipnir214,

i did verify that I only spelled "Yellow" once in the very begin to insert it into an array $RouteOne. Then I insert that array into an assoc array at indexes [1][5]. Then during Enact_Turn function I assign $Player_Array[1][5] to a varable $myRteArray then I assing it to something called $Array then I start by telling the user they are at posting $Array[0]. And this all works. But there is something in the the string that will not allow me to compare...

I am open to any ideas as a work around or fix to this issue.

Thank you.
 
I figured it out. I am not sure how to fix it though. The variable $saveNewPos is in an array format for Yellow. What i mean is $saveNewPos[0] = Y, $saveNewPos[1] = e ... etc.

I assume that becase I am using $Array[$i] to loop through my array or something. Is there a way I can somehow create a new variable to create the word Yellow again from an array like the one above?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top