not quite variable variables

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
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

not quite variable variables

Post by Unipus »

I want to pass the name of a variable in a function, then have that refer to an array variable and make some totally awesome happenings happen. A pseudo-example:

Code: Select all

function get_loaded($type)
{
$how_many = count($type);
die($how_many);
}
$how_many never gets a value.

Several valid examples for $type are already set before this function is set. So for instance, if I call get_loaded("widgets"), I want to count the array values in $widget, already set:

Code: Select all

$widget["floaty"] = "magnificent floaty";
$widget["smackbox"] = "this sure is content.";
$widget["dracula"] = "hanging from the ceiling";
variable variables are not the answer I once dreamed they were, and concating doesn't seem to do the trick either.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Example.

Code: Select all

<?php

function get_loaded($type)
{
  global $$type;
  $how_many = count($$type);
  die("$how_many");
}

$widgets = array();
$widgets[] = 'one';
$widgets[] = 'two';
get_loaded('widgets');

?>
So variable variables are the answer, but die($how_many) wasn't ;)
As $how_many is an integer value, the die() will be given an int, so it will treat it as an exit code. You need to have $how_many be treated as a string for die() to display it, hence the quotes in die("$how_many");
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

Gotcha. I see now.

The only thing I don't entirely get is why is global $$type necessary? $type is passed to the function by definition... so I don't get that.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$type is just the name of the variable. To bring the actual variable into scope you need global $$type

But it does raise the question of why you don't just pass the actual variable in, or even just use count() .. ie no function required, but i'm guessing this is just a test that will have some other use ?

[edit] Never mind, i just saw the 'pseudo-example' bit ;)
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

much thanks.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

markl999 wrote:But it does raise the question of why you don't just pass the actual variable in, or even just use count() .. ie no function required
My thoughts exactaly.
Post Reply