Dynamic Vars with spaces...

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
Tsukassa
Forum Newbie
Posts: 3
Joined: Mon Nov 07, 2005 5:41 pm

Dynamic Vars with spaces...

Post by Tsukassa »

Maybe you guys will laugh at me, but I thought I would be able to create my vars directly from the my mysql data base, here is the script :

Code: Select all

for($k=0;$k<count($keys);$k++){
				$this->$keys[$k]=$myItem[$keys[$k]];
			}
At first it seem a great idea, but as you guys probably figured out, some of my keys[$k] contains space. It works, even if the var name has a space, but I wanted to know if there was a way to access it... a var you can't access isnt really useful you know...

by the way, its my first post, and its not going to be my last! :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

technically, there is a way to access it, but it's far easier to deal with them if they don't have illegal characters in them.. basically anything that isn't alphanumeric or an underscore should be stripped out..
Tsukassa
Forum Newbie
Posts: 3
Joined: Mon Nov 07, 2005 5:41 pm

Post by Tsukassa »

fine, I'll use normal vars, but I'm curious you see, how could I access those vars?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

[feyd@work]>php -r "$a = new stdClass; $a->{'some var'} = 'twins!'; var_export($a);"
class stdClass {
  public $some var = 'twins!';
}
Tsukassa
Forum Newbie
Posts: 3
Joined: Mon Nov 07, 2005 5:41 pm

Post by Tsukassa »

Thanks for your fast response :)
Post Reply