thecommonthread Posted November 24, 2010 Share Posted November 24, 2010 Hi, so I have an easy problem that for some reason I couldn't find an answer to anywhere. :'( 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. Quote Link to comment Share on other sites More sharing options...
seanlim Posted November 24, 2010 Share Posted November 24, 2010 ${"pic".$pic_number."fileName"} You would be better off using an array though, $picfileName[1] $picfileName[2] $picfileName[3] $picfileName[4] and then you can simply access them using $picfileName[$pic_number] Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 24, 2010 Share Posted November 24, 2010 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. Quote Link to comment Share on other sites More sharing options...
trq Posted November 24, 2010 Share Posted November 24, 2010 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. It's a bit like that isn't it? Weird. Quote Link to comment Share on other sites More sharing options...
thecommonthread Posted November 24, 2010 Author Share Posted November 24, 2010 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 24, 2010 Share Posted November 24, 2010 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? Quote Link to comment Share on other sites More sharing options...
thecommonthread Posted November 24, 2010 Author Share Posted November 24, 2010 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. Quote Link to comment Share on other sites More sharing options...
seanlim Posted November 24, 2010 Share Posted November 24, 2010 wasn't that already in my first reply? ${"pic".$pic_number."fileName"} Quote Link to comment Share on other sites More sharing options...
thecommonthread Posted November 24, 2010 Author Share Posted November 24, 2010 Yes. lol I overlooked it because my mind registered it as some quote or reply that didn't matter. THANK YOU! Quote Link to comment Share on other sites More sharing options...
trq Posted November 24, 2010 Share Posted November 24, 2010 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. Quote Link to comment Share on other sites More sharing options...
seanlim Posted November 24, 2010 Share Posted November 24, 2010 At least we've already done our part proposing the better alternative... I don't advocate such use of variable variable names, although that *technically* would work Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 24, 2010 Share Posted November 24, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.