Page 1 of 1

Setting a variable to be the text of another variable.

Posted: Thu Aug 04, 2011 11:22 am
by ThatPerson
I am making a friends list which uses a loop, and it has a variable, $a, which goes up two each time. Is there a way that I could set a variable to be p followed by $a, such as:

$p2
$p4
$p6

I did think about $"p".$a but that did not work. Can anybody shed some light on this matter?

Code: Select all

<?php
echo <<<END
<input type="Submit" value = "Echo" name ="Submitter">
END;
$fr = "hello"; #Is a MySQL query, but as I am just wondering about the variables, it is not needed.
$len = strlen($fr);
$a = 0;
for ($i = 1; $i <= $len; $i++) {
$a = $a +2;
$"p".2 = $a #This is the line which should be able to update
}
$len2 = $len/2;
if (isset($_POST['Submitter'])) {
$p = 0;
for ($i = 1; $i <= $len2; $i++) {
$p = $p+2;
echo $"p".$p;
}
}
?>
If you are wondering why I need this, it is to create a friends list where there will also be buttons which need to be able to link exclusively to the variables where there can be different amounts of variables.

Re: Setting a variable to be the text of another variable.

Posted: Thu Aug 04, 2011 12:23 pm
by ThatPerson
Never mind, I found out. I just needed {} around the statement.

Re: Setting a variable to be the text of another variable.

Posted: Thu Aug 04, 2011 12:23 pm
by AbraCadaver
Use an array. It will be much easier to use in almost all cases. I can't determine what you are trying to do from your code so I don't have an example.