Concatenate varriables

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
justbeingme3
Forum Newbie
Posts: 2
Joined: Wed Feb 17, 2010 4:25 pm

Concatenate varriables

Post by justbeingme3 »

Hi my name is Chris and i am currently underway of creating a web hosting site for my personal use as like a secondary buissness if you like but please dont ask to much about that but i have stumbled upon a dilema that includes a couple of varriables in a loop as i am trying to make it so my varriable is created automaticly acording to a number so for example...

Code: Select all

$mytext = $endtext[and my varriable here]
so basicly i am outputting a number and depending on what that number is it creates a varriable i thought of concatenating the varriable's so i get the result varriable if you would like to see my code to further understand what i am doing please post back i will be happy to help but yes i am trying to make choose a varriable from a string so again for those who still dont understand i have a number that is forever changing lets say 6 and i want to choose a varriable that will have that number in it so my varriable is $understandme[MY STRING HERE] and just so you know i am using the explode function to work with thank you in advanced.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Concatenate varriables

Post by flying_circus »

I am unclear on what exactly you are trying to do, but it sounds like you want to create a variable variable name.

You can do that, and the manual is here: http://www.php.net/manual/en/language.v ... riable.php

Code: Select all

$my_variable_here = "123";
$var_name = "endtext_{$my_variable_here}";
$$var_name = "my value";
 
print $endtext_123;  //prints "my value"
Personally, I think that is pretty messy code, and will just store my data to an array

Code: Select all

$my_variable_here = "123";
$array_of_vars = array();
$array_of_vars["endtext_{$my_variable_here}"] = "my value";
 
print $array_of_vars["endtext_123"]; // prints "my value"
justbeingme3
Forum Newbie
Posts: 2
Joined: Wed Feb 17, 2010 4:25 pm

Re: Concatenate varriables

Post by justbeingme3 »

Yes thank you that is what i have been looking for and i agree with you that it is pretty messy but however i need to be able to store my data in an array to control a javascript menu that reads data from an array thank you anyway.
Post Reply