Jump to content

dynamic var.


dcole.ath.cx

Recommended Posts

can I have a var in a var, then have it in a for statement that counts up..like have if I had:$group1person$group2person$group3person$group4personand instead have for ($i = 0, $i<5, $i++){($group($i)person) ...}...then use that $group($i)person for more PHP. I'm looking for a way so I don't have to write out each var. with a number.

Link to comment
Share on other sites

this statement that I'm working on is getting to hard... I know have to put that within 5 inner going for and if statements that have aton of var. that need the same $ican I use $group{$i}personwell I'll be back tomorrow, maybe I'll use that first way... I'm going to need time to think about it...

Link to comment
Share on other sites

You can just make the name of the variable you want inside a string (without the first $), and then reference it as a variable variable like I showed above.

$var = "hello";$$var = "test";//now $hello == "test"

Link to comment
Share on other sites

I thought I answered your question, that is how you reference a variable with a name like "$group2person" or "$group3person" or some variable where you programmatically determine the name. What are you trying to do?

Link to comment
Share on other sites

I still have that 4 quote thing, it's the advanced post.well I needed for something like:$group{$i}person$joe{$i}sun$bob{$i}mon...It did help a little bit, it condenced 100 lines of code into 20.There may have been an easier way but this way keeps my code looking alike.

Link to comment
Share on other sites

I don't think there are 4 different types of quotes. There are single-quotes and double-quotes, what else? There aren't triple or quadruple quotes, and a backtick (`) isn't a quote at all. In fact, in PHP, a backtick does something substantially different then a quote.If you think you need 4 different types of quotes, you might want to re-think your design..

Link to comment
Share on other sites

what aspnetguy said, I needed to use the \' and \" but it turns out I had to use them in the correct order... because if I didn't it got messed up, so that's why there was such a big deal abour 4 quotes.

Link to comment
Share on other sites

Oh, I wouldn't consider an escaped quote another "type" of quote. A ' and a \' are the same, they are both single quotes, but one is escaped and one is not, it just depends where you use it.I can't for the life of me remember which project I'm thinking of, but I did have some pretty nasty-looking echo statements for something. I would echo some javascript code, inside of a quoted HTML attribute, where the javascript function call has quotes around the parameters, and the parameters themselves had escaped quotes. It was something like 8 slashes followed by a quote. Pretty ugly.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...