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

Link to comment
Share on other sites

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