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!

Variable names in variables 1

Status
Not open for further replies.

theoneweasel77

Programmer
Aug 10, 2004
54
0
0
US
Is it possible to store variable names in variables. I remember reading about that somewhere but I can't remember how to do it. Thanks in advance
 
e.g. The variable is $myVar = 1, and $varHolder = "$myVar"
I'm not sure if this is indicative of what you are actually doing, but on the assumption it is, you're not quite assigning the variable correctly. $varHolder = "$myVar" will not set $varHolder to "myVar", but instead it will set $varHolder to the value of $myVar, which in your example is 1. (Remember, PHP variables are evaluated/processed/whatever inside of "".) $varHolder = '$myVar' won't work either. What you need to do is simply set $varHolder to "myVar" (no $) and $$varHolder will give you the value of $myVar.


Lite...
 
that's what I'm doing. I've got a function returning a string, which is stored in a variable $terrain. I then try to output $$terrain, and it prints $field or $forest, the contents of the variable $terrain, not the values of the stored variable name.
Code:
function piece_type($piece, $color)
	{
		if ($color == "b")
			$color == "blue";
		else
			$color == "red";
		print $color;
		if ($piece == "infantry1")
			return "infantry_".$color."triangle.gif";
		elseif ($piece == "infantry2")
			return "infantry_".$color."circle.gif";
		elseif ($piece == "infantry3")
			return "infantry_".$color."square.gif";
		elseif ($piece == "cavalry1")
			return "cavalry_".$color."triangle.gif";
		elseif ($piece == "cavalry2")
			return "cavalry_".$color."circle.gif";
		elseif ($piece == "cavalry3")
			return "cavlary_".$color."square.gif";
		elseif ($piece == "general")
			return "general_".$color.".gif";
		elseif ($piece == "guerilla")
			return "infantry_".$color."diamond.gif";
		elseif ($piece == "artillary")
			return "artillary_".$color.".gif";
		else
			return "no such piece found";
	}
Code:
$terrain = field_or_forest($var.$i);
print ("<td width=71 height=71 style=\"$$terrain\" align=center background=\"/games/images/$image\" style=\"background-repeat:no-repeat; background-position: 7px 7px\;\">");
output taken directly from the source code:
Code:
<td width=71 height=71 style="$field" align=center background="/games/images/tp.gif" style="background-repeat:no-repeat; background-position: 7px 7px\;">c4</td>
 
One thing that's going to cause you problems is that you're confusing the assignment operator ("=") with the comparison operator ("==")

Elseif is evil and should never be used. In your particular use, a switch is more readable and maintainable.

As a good general practice in software engineering, a function should have only one exit point. It doesn't matter much for a simple function like this one, but a complex function with multiple exit points can be difficult to debug.

Code:
function piece_type($piece, $color)
{
	if ($color == "b")
		$color == "blue";
	else
		$color == "red";

 	print $color;

	switch ($piece)
	{
		case "infantry1":
            	$retval = "infantry_".$color."triangle.gif";
			break;
		case "infantry2":
			$retval = "infantry_".$color."circle.gif";
			break;
		case "infantry3":
			$retval = "infantry_".$color."square.gif";
			break;
		case "cavalry1":
			$retval = "cavalry_".$color."triangle.gif";
			break;
		case "cavalry2":
			$retval = "cavalry_".$color."circle.gif";
			break;
		case "cavalry3":
			$retval = "cavlary_".$color."square.gif";
			break;
		case: "general":
	            $retval = "general_".$color.".gif";
			break;
		case "guerilla":
			$retval = "infantry_".$color."diamond.gif";
		case "artillary":
			$retval = "artillary_".$color.".gif";
			break;
       	default:
	            $retval = "no such piece found";
	}

	return $retval;
}

I'm not sure what it is you're trying to do and why you are trying to use variable variables, but variable variables are documented in the PHP online manual here.

I am confused particularly by your posting the code for piece_type(), since I don't see where in your other code you are invoking it.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
oops, sorry. Meant to include the worlds most massive if statement :)
Code:
if ($pos == "a1" || $pos == "a2" || $pos == "a3" || $pos == "a4" || $pos == "b1" || $pos == "b2" || $pos == "b3"
		|| $pos == "b4" || $pos == "b5" || $pos == "b7" || $pos == "b10" || $pos == "c1" || $pos == "c3" || $pos == "c5"
		|| $pos == "c6" || $pos == "c8" || $pos == "c9" || $pos == "c11" || $pos == "d1" || $pos == "d2" || $pos == "d4"
		|| $pos == "d5" || $pos == "d6" || $pos == "d7" || $pos == "d11" || $pos == "e1" || $pos == "e2" || $pos == "e4"
		|| $pos == "e5" || $pos == "e6" || $pos == "e7" || $pos == "e9" || $pos == "e10" || $pos == "e11" || $pos == "f1"
		|| $pos == "f3" || $pos == "f4" || $pos == "f5" || $pos == "f6" || $pos == "f7" || $pos == "f8" || $pos == "f9"
		|| $pos == "f11" || $pos == "g2" || $pos == "g3" || $pos == "g5" || $pos == "g6" || $pos == "g7" || $pos == "g8"
		|| $pos == "g10" || $pos == "g11" || $pos == "h1" || $pos == "h5" || $pos == "h6" || $pos == "h7" || $pos == "h8"
		|| $pos == "h10" || $pos == "h11" || $pos == "i1" || $pos == "i3" || $pos == "i4" || $pos == "i6" || $pos == "i7"
		|| $pos == "i9" || $pos == "i11" || $pos == "j2" || $pos == "j5" || $pos == "j7" || $pos == "j8" || $pos == "j9"
		|| $pos == "j10" || $pos == "j11" || $pos == "k8" || $pos == "k9" || $pos == "k10" || $pos == "k11")
			return "forest";
		else
			return "field";
 
As an addendum, I would have coded field_or_forest() as:

Code:
function field_or_forest($pos)
{
	$retval = 'field';
	switch ($pos[0])
	{
		case  'a':
			$possible_values = array (1, 2, 3, 4);
			break;
		case  'b':
			$possible_values = array (1, 2, 3, 4, 5, 7, 10);
			break;
		case  'c':
			$possible_values = array (1, 3, 5, 6, 8, 9, 11);
			break;
		case  'd':
			$possible_values = array (1, 2, 4, 5, 6, 7, 11);
			break;
		case  'e':
			$possible_values = array (5, 6, 7, 9, 10, 11);
			break;
		case  'f':
			$possible_values = array (1, 3, 4, 5, 6, 7, 8, 9, 11);
			break;
		case  'g':
			$possible_values = array (2, 3, 5, 6, 7, 8, 10, 11);
			break;
		case  'h':
			$possible_values = array (1, 5, 6, 7, 8, 10, 11);
			break;
		case  'i':
			$possible_values = array (1, 3, 4, 6, 7, 9, 11);
			break;
		case  'j':
			$possible_values = array (2, 5, 7, 8, 9, 10, 11);
			break;
		case  'k':
			$possible_values = array (8, 9, 10, 11);
			break;
		default:
			$possible_values = array();
	}

	if (in_array($pos[1], $possible_values)
	{
		$retval = 'forest';
	}

	return $retval;
}

I think it's a bit more maintainable.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top