Jump to content

Using a variable to dynamically specify a different variable


thecommonthread

Recommended Posts

Hi, so I have an easy problem that for some reason I couldn't find an answer to anywhere. :wtf: :'( >:(

 

I have a bunch of variables like this:

 

  • $pic1fileName
  • $pic2fileName
  • $pic3fileName
  • $pic4fileName
  • $pic5fileName...you get the idea

 

So I have another variable I'm pulling from a database that specifies which number to show, so I need a variable something like this:

 

$pic($pic_number)fileName

 

I just don't know what the proper syntax is.  Anyone?  Thank you freaking much for any help; this is a lame problem.

 

Link to comment
Share on other sites

So, are all you guys/gals that are asking this same question taking the same programming class? This is like the 5th thread on this subject in the past day.

 

If your instructor is using an example or is suggesting creating either a series of named program variables or creating program variables who's name is from a value in a database table, suggest back to him/her that THEY SHOULD BE TEACHING HOW TO USE ARRAYS. Blindly polluting the main program variable space based on database table column names or names held as data IS BAD because now you must keep track of what variables your program is using AND the names of columns/data values in your database so that you don't overwrite your main program variables simply because you forgot and added a column or saved a value in your table that you are already using as a variable.

 

Short answer: You should be using an array, not a series of named variables to hold a set of related data. Arrays are for sets of data.

Link to comment
Share on other sites

Sorry for the multiple post, but I couldn't find the answer I was looking for in the past posts.  An array is not ideal in the specific placement of my code I am using since using an array I don't know how to stick the array entry number into the middle of a variable (like $the[$color]Pill), so I'm looking for an alternative syntax.

 

So if you could PLEASE help me out, I would appreciate it so much, you have no idea.  I am stuck without the arrays option in an odd series of events.

 

So example: If I have two different variables named $theBluePill and $theRedPill and using another variable named $color that equals "Red", I want to dynamically call $theRedPill by sticking $color into the variable.

Link to comment
Share on other sites

What we are suggesting using an array -

$thePill['Blue'] = 'some value for Blue';
$thePill['Red'] = 'some other value for Red';

...

$color = 'Blue'; // value retrieved from your database table...
echo $thePill[$color]; // outputs 'some value for Blue'

 

And given that your example changed radically from the first post to the last post, what are you actually trying to accomplish?

Link to comment
Share on other sites

Listen, I appreciate your insight on how to program an array, but I understand arrays and I am trying to find a different method.  My example was just another example of the same thing: attempting to do close to the same thing as with an array except placing the object that makes the variable dynamic in the middle of the variable, not the end.  I was trying to make it simple and this thread has been completely about arrays, which is the opposite of what I am trying to use in this situation.

 

Can this be done??  Can you take the value of a variable and stick it into another variable to specify another certain variable?

 

Another example: $box{$num}detail - what is the proper syntax outside of the array method?  Obviously it's different than sticking {$num} in the center of the var.

Link to comment
Share on other sites

Yes.  lol

 

I overlooked it because my mind registered it as some quote or reply that didn't matter.

 

THANK YOU!

 

So your not willing to take the advice of people who know what they are doing? Do you understand the consequences of creating variables dynamically? Obviously not.

 

Have fun debugging.

Link to comment
Share on other sites

LOL, this thread is almost identical to one from a few days ago, where it was necessary to point out to the OP that it is human nature, when trying to help someone, to suggest better ways of accomplishing a task (i.e. quicker, simpler, less code, less memory, more flexible, more reusable...), especially when the reason you are asking a specific group of people is because you want to benefit from their experience in a subject.

 

To the OP, if all you were interested in is finding "what the proper syntax is" for something you were doing, you should probably not be asking a group of programmers for help, especially if you are overlooking the posted answer as something that didn't matter. You should be researching the information yourself in the documentation.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.