/    Sign up×
Community /Pin to ProfileBookmark

PHP variable variables

I am very agrivated by this and it is taking everything in me not to throw something.

[code=php]
${$temp . “[‘passyds’]”} += $passing[$i + 3];
print ${$temp . “[‘passyds’]”} . ” ” . $C0Palmer[‘passyds’];

${$temp . “[‘passyds’]”} prints the correct number
$C0Palmer[‘passyds’]; prints nothing
[/code]

$temp is definately C0Palmer. I did a strlen and it was the correct length(8) and I did print $temp and it showed that it was C0Palmer. I printed a list of all the variables and it showed that C0Palmer[‘passyds’] was there. Obviously there is a naming issue with the array but I have completely run out of ideas on how to find what the difference is. Any help would be much appreciated.

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@chrysSep 28.2005 — Whatcha tryin to do?

An alternative to variable variables is using an array.. like

$vars["$variablename"] = $variablename;

GImme more details ?
Copy linkTweet thisAlerts:
@resullivanauthorSep 28.2005 — The reason I can not do that is because the name like C0Palmer is going to have stats within the array $C0Palmer for examle $C0Palmer['passyds'].
Copy linkTweet thisAlerts:
@resullivanauthorSep 28.2005 — Here is a bigger slice

[code=php]
$passing = explode(" ", $temppass);
$num = count($passing);
for ($i = -10; $i < $num; $i += 10) {
$temp = trim(str_replace(".", "", $passing[$i]));
if (strlen($temp) != 0) {
${$temp . "['comp']"} += $passing[$i + 1];
${$temp . "['att']"} += $passing[$i + 2];
${$temp . "['passyds']"} += $passing[$i + 3];
print ${$temp . "['passyds']"} . " " . strlen($temp) . " " . $temp;
${$temp . "['y/a']"} += $passing[$i + 5];
${$temp . "['sack']"} += $passing[$i + 6];
${$temp . "['ydsl']"} += $passing[$i + 7];
${$temp . "['td']"} += $passing[$i + 8];
${$temp . "['int']"} += $passing[$i + 9];
}
}
[/code]


small explanation:

$temppass has a persons name then stats in row until another persons name comes up followed by their stats all seperated by spaces. The print statement is there for debugging purposes. ${$temp . "['passyds']"} prints the correct stat however if I look at the persons name for example C0Palmer and try $C0Palmer['passyds'] it returns nothing. I did a print_r of all defined variables and C0Palmer['passyds'] was one of them.
Copy linkTweet thisAlerts:
@chrysSep 28.2005 — I believe it's a poor way to code (using variable variables).

What you can do is create a "person" object.

This way you can have $people[$lastname] be a refernce to an object of stats, or even another array...

so if I understand correctly, then you can do:

$people['jones']->sacks = $sacks;

$people['jones']->yards = $yards;

Do you capt?
Copy linkTweet thisAlerts:
@WebnerdSep 28.2005 — $temp="craig";

${$temp}=Array();

${$temp}['passyds'] ="is cool";

echo $craig["passyds"];


Works fine. But I agree, an object would be a lot better way to go.
Copy linkTweet thisAlerts:
@resullivanauthorSep 28.2005 — I will try that I just did not know of what you mentioned. Unfortanatly I will not be able to test it until sunday. I play fanatasy football on yahoo but you have to pay like 9.99 to be able to view live stats. Im cheap so i decided to make a script to view live stats via yahoo. You can go under an individual game to see individual player stats so I made a script that goes to yahoos nfl sports page. It find the active or just finished games and gathers the players stats for those games. It does all this fine but I have to clean up the data so I can use it. Actually I know a url of a previously played game so I am going to go force it into the script and try what you mentioned. Thanks!

p.s.

Hopefully this works because php variable variables agrivate me.
Copy linkTweet thisAlerts:
@WebnerdSep 28.2005 — [code=php]
<?

class Person{

var $name;
var $stats;

function Person($name){
$this->name=$name;
$this->stats=Array();
}

function AssignStat($stat,$value){
$this->stats[$stat]=$value;
}

}


// Set the Object

$temp="COPalmer";

$p=new Person($temp);
$p->AssignStat("comp",$passing[$i + 1]);
$p->AssignStat("att",$passing[$i + 2]);
$p->AssignStat("passyards",$passing[$i + 3]);
// etc...

// Print the Object

echo $p->name;

foreach($p->stats as $key=>$value){
echo $key."=".$value."<br/>";
}




?>[/code]
Copy linkTweet thisAlerts:
@resullivanauthorSep 28.2005 — [code=php]
$temp="craig";

${$temp}=Array();

${$temp}['passyds'] ="is cool";

echo $craig["passyds"];
[/code]


I see a difference from this and what I have. If i made it like this it would probably work but I like to learn new things so I am going to do it the object way.

${$temp}['passyds'] ="is cool"; yours

${$temp . "['passyds']"} ="is cool"; mine
Copy linkTweet thisAlerts:
@resullivanauthorSep 28.2005 — First off I would like to thank you for not flamming me about not using obeject oriented programming. Second, I know that this is the best way to do things but is tuff to make that transition when you do it more for fun than as a job. I have always used functions but have never really made that transition into fully object oriented.
Copy linkTweet thisAlerts:
@chrysSep 28.2005 — [code=php]
<?

class Person{

var $name;
var $stats;

function Person($name){
$this->name=$name;
$this->stats=Array();
}

function AssignStat($stat,$value){
$this->stats[$stat]=$value;
}

}


// Set the Object

$temp="COPalmer";

$p=new Person($temp);
$p->AssignStat("comp",$passing[$i + 1]);
$p->AssignStat("att",$passing[$i + 2]);
$p->AssignStat("passyards",$passing[$i + 3]);
// etc...

// Print the Object

echo $p->name;

foreach($p->stats as $key=>$value){
echo $key."=".$value."<br/>";
}




?>[/code]
[/QUOTE]


Yep, this is the right way to do it. Webnerd, can you help me out with my PHP compilation issue? It's driving me up the friggin wall.
Copy linkTweet thisAlerts:
@resullivanauthorSep 28.2005 — so after the above is done how would I get C0Palmer passyds? Would it be

C0Palmer->stats['passyds']?
Copy linkTweet thisAlerts:
@chrysSep 28.2005 — so after the above is done how would I get C0Palmer passyds? Would it be

C0Palmer->stats['passyds']?[/QUOTE]


Well, the way I'd do it (using that class) is:

$players['C0Palmer'] = new Player();

Then you access stats like

$players[C0Palmer]->passyds;
Copy linkTweet thisAlerts:
@resullivanauthorSep 28.2005 — Maybe I am wrong but it seems as if I am going to need to still use a variable variable which seems to be for example $p. The reason is $temp is something that changes as the loop progresses. The array that is being looped through may look something like this:

C0Palmer





100

10

AnotherName

1

10




I do not see how I am going to be able to call back a specific stat at a later time based on the players name. In the above $p would refer to C0Palmer(name, stats) but there are going to be multiple names.
Copy linkTweet thisAlerts:
@resullivanauthorSep 28.2005 — Just let me know if these changes I have made are completely stupid but from my small understanding of programming this is what I have changed.

[code=php]
class Person{

var $name;
var $stats;

function Person($name){
$this->name=$name;
$this->stats=Array();
}

function Person(){
$this->stats=Array();
}

function AssignStat($stat,$value){
$this->stats[$stat]+=$value;
}

}

$passing = explode(" ", $temppass);
$num = count($passing);
for ($i = -10; $i < $num; $i += 10) {
$temp = trim(str_replace(".", "", $passing[$i]));
if (strlen($temp) != 0) {
if (!isset(${$temp})) {
${$temp}=new Person(str_replace("0", ". ", $temp));
}
${$temp}->AssignStat("comp",$passing[$i + 1]);
${$temp}->AssignStat("att",$passing[$i + 2]);
${$temp}->AssignStat("passyards",$passing[$i + 3]);
${$temp}->AssignStat("y/a",$passing[$i + 5]);
${$temp}->AssignStat("sack",$passing[$i + 6]);
${$temp}->AssignStat("ydsl",$passing[$i + 7]);
${$temp}->AssignStat("td",$passing[$i + 8]);
${$temp}->AssignStat("int",$passing[$i + 9]);
}
}
[/code]
Copy linkTweet thisAlerts:
@resullivanauthorSep 28.2005 — I do not know if it is the best way but the above works. Thank you for all the help.
Copy linkTweet thisAlerts:
@WebnerdSep 28.2005 — [code=php]
<?
class Person{

var $name;
var $stats;

function Person($name){
$this->name=$name;
$this->stats=Array();
}

function Person(){
$this->stats=Array();
}

function AssignStat($stat,$value){
$this->stats[$stat]+=$value;
}

}


$people=Array();

$passing = explode(" ", $temppass);

$num = count($passing);

for ($i = -10; $i < $num; $i += 10) {

$temp = trim(str_replace(".", "", $passing[$i]));

if (strlen($temp) != 0) {

$name=str_replace("0", ". ", $temp);

if(!isset($people[$name])){
$people[$name]=new Person($name);
}
}

$people[$name]->AssignStat("comp",$passing[$i + 1]);
$people[$name]->AssignStat("att",$passing[$i + 2]);
$people[$name]->AssignStat("passyards",$passing[$i + 3]);
$people[$name]->AssignStat("y/a",$passing[$i + 5]);
$people[$name]->AssignStat("sack",$passing[$i + 6]);
$people[$name]->AssignStat("ydsl",$passing[$i + 7]);
$people[$name]->AssignStat("td",$passing[$i + 8]);
$people[$name]->AssignStat("int",$passing[$i + 9]);
}


print_r($people);

?>
[/code]


You access each player by

[code=php]
$players["playername"];
[/code]
×

Success!

Help @resullivan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...