Page 1 of 1

variable name [SOLVED]

Posted: Sat Sep 15, 2007 2:10 am
by nayeemmz
Hi,

I am having a problem which someone in this forum might be able to help me with.
I want to have a variable with a variable in its name

For e.g my if I have a loop

Code: Select all

for($num=0;$num<10;$num++)
I want to have a variable name like

inputdata$num which means 10 variables should be formed after the loop terminates
inputdata1, inputdata2......inputdata10.


I am unable to do this.

Can someone help please?


Thanks

Posted: Sat Sep 15, 2007 2:12 am
by s.dot
Check out variable variables in the php manual.

Code: Select all

$num = 4;
${'inputdata' . $num} = 'gobblygooooo';

echo $inputdata4;  // echo's gobblygooooo

Posted: Sat Sep 15, 2007 2:31 am
by volka
You can also use an array

Code: Select all

<?php
$inputdata = array();
$inputdata[] = 'xyz';

Posted: Sat Sep 15, 2007 3:34 am
by Kieran Huggins
I've found using a keyed array (a hash, really) is way less prone to errors. Plus you get all the benefits of the array functions for checking existence etc...

Posted: Sat Sep 15, 2007 12:03 pm
by elinews
I just had this same problem. It's a really quick fix. I think this is what I did:

Code: Select all

$inputdata[$num]="whatever";

Posted: Sat Sep 15, 2007 4:09 pm
by nayeemmz
Thankyou all.

Your solutions work.

I really appreciate it.