Jump to content

Variable variables... I think


foreverhex

Recommended Posts

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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.