Setting a variable to be the text of another variable.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Setting a variable to be the text of another variable.

Post 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.
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

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

Post by ThatPerson »

Never mind, I found out. I just needed {} around the statement.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply