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? Link to comment https://forums.phpfreaks.com/topic/100414-variable-variables-i-think/ 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]; } Link to comment https://forums.phpfreaks.com/topic/100414-variable-variables-i-think/#findComment-513524 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. Link to comment https://forums.phpfreaks.com/topic/100414-variable-variables-i-think/#findComment-513542 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 Link to comment https://forums.phpfreaks.com/topic/100414-variable-variables-i-think/#findComment-513543 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. Link to comment https://forums.phpfreaks.com/topic/100414-variable-variables-i-think/#findComment-513545 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. Link to comment https://forums.phpfreaks.com/topic/100414-variable-variables-i-think/#findComment-513565 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.