Weird situation processing a variable with a 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
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Weird situation processing a variable with a variable...

Post by Wolf_22 »

Forgive me if I end up confusing you guys more than myself, but I'm up against a wall here with the use of a variable that I'm trying to get initialized with the value of an array (if I understand it all correctly). See below:

Code: Select all

      foreach($ad as $key=>$val){
            $id = $val->ID;
            $set = 'today-'.$id;
            echo $set;
      }
To no surprise to many of you, it's not working and I can't seem to figure out a way to process the $val->ID prior to initializing the variable "$id." I know I'm missing something here (and probably something fundamental), but I just can't see it. Feel free to educate me.

Update: I just naively realized that the right arrow indicates an object...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Weird situation processing a variable with a variable...

Post by requinix »

I, for one, don't know what you're trying to do...
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Weird situation processing a variable with a variable...

Post by Wolf_22 »

I'm sorry. I have a tendency of looking too deeply into things at times.

Code: Select all

$test = "today-$val->ID";
Maybe you could tell me what the above is? Is is just shotty code or is there something going on with an object and a variable. I know, I know, it's a horrible way to ask something like this but I can't help it. :banghead:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Weird situation processing a variable with a variable...

Post by requinix »

Code: Select all

$test = "today-$val->ID";
"today-$val->ID" is a string, but since it uses "s PHP sees the $ and starts looking for a valid variable name. It sees the "val", continues with the "->", and ends with "ID": the whole thing is a variable it can insert.
That line is equivalent to

Code: Select all

$test = "today-" . $val->ID;
So if $val->ID is 5 then $test = "today-5".


Your confusion is partly why some programmers like a slightly different syntax:

Code: Select all

$test = "today-{$val->ID}";
It's a little more obvious what that's supposed to do.
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Weird situation processing a variable with a variable...

Post by Wolf_22 »

Okay, that clears some of this up. After looking at my code, though, I've come up with another question pertaining to arrays.

An array basically consists of "keys" and "values." Creating an array is pretty simple in that you simply create a variable like so "$var[] = whatever." My question is this: how can I not only insert values, but also, control the keys?

If I have the array "$admins[]" and let's say I wish to assign each admin a name, but for their keys, I would like to assign an ID, how could I do this? The reason I'm confused about doing it is because in the past, I've only needed to do the simple assignment where you give the array a bunch of values. Is there a function appropriate for what I'm wanting to do?
Post Reply