foreverhex Posted April 10, 2008 Share Posted April 10, 2008 I know Im getting into strange territory, put I need to make a superglobal have a variable that I am looping in a while statement. I think I need to use a variable variable. I figured I needed help because my brain feels backwards. What I want to do is make basically: <?php $ind_id = $_POST[ind_id $num ]; ?> i already know that this is wrong. But its what I'm try to do so that the variable would say $_POST[ind_id1], $_POST[ind_id2] and so on. Is there a way to do this? Or am I backwards and need to do this a completely different way? Quote Link to comment Share on other sites More sharing options...
marcus Posted April 10, 2008 Share Posted April 10, 2008 for($i=1;$i<=5;$i++){ $post[$i] = $_POST['ind_id' . $i]; } Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 10, 2008 Share Posted April 10, 2008 If you are writing code that is using sequentially named variables for a set of related data, you should be using an array. Using an array will result in the simplest code and you won't need to carry around a value telling you how many of them there are. With an array, a simple foreach() loop will iterate over all the values and if you happen to want to know how many values there are, you can use the count() function. Also, while the code mgallforever posted is not using variable variables, a variable variable takes 3 times longer to access than an array element. Quote Link to comment Share on other sites More sharing options...
foreverhex Posted April 10, 2008 Author Share Posted April 10, 2008 for: mgallforever Hey thanks for the suggestion, Im always reluctant to use for and for each. Its working now! Are you really only 15? for: PFMaBiSmAd I didnt put my whole code down. There are a bunch of variables that I am using and my initial idea was to use multi-dimension arrays but that would be a lot more coding than needed. Thanks though Quote Link to comment Share on other sites More sharing options...
blackcell Posted April 10, 2008 Share Posted April 10, 2008 Hey just advice from a novice in learning when it comes to php, I hated the idea of using arrays at first. I came to a situation where I had to use them and now I like to deal with them more. Quote Link to comment Share on other sites More sharing options...
foreverhex Posted April 10, 2008 Author Share Posted April 10, 2008 Dont get me wrong. I use arrays a lot, it just wouldnt be efficient to use then for this script... a lot of implodes and explodes. 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.