pakenney38 Posted July 10, 2006 Share Posted July 10, 2006 For example, I got this to work:[code]<?PHPfor ($counter = 1; $counter <= 200; $counter++){ $T[$counter] = 'T' . $counter; echo $T[$counter] . '<br>';}if ($T[1]){echo 'yes<br>';}else{echo 'no<br>';}if ($T[2]){echo 'yes<br>';}else{echo 'no<br>';}?>[/code]But what if I didn't want my variables to come out named $T[1], $T[2], $T[3]....etc?What if I wanted $T1, $T2, $T3....etc?How do I do this without setting each variable $T1 through $T200 independently? Quote Link to comment https://forums.phpfreaks.com/topic/14141-setting-vars-with-recurring-numbers-within-the-vars/ Share on other sites More sharing options...
kenrbnsn Posted July 10, 2006 Share Posted July 10, 2006 Why would you want individual variables instead of an array? Arrays are so much easier to use.BTW, please change the tags around your code from [nobbc][quote][/quote][/nobbc] to [nobbc][code][/code][/nobbc]Ken Quote Link to comment https://forums.phpfreaks.com/topic/14141-setting-vars-with-recurring-numbers-within-the-vars/#findComment-55404 Share on other sites More sharing options...
pakenney38 Posted July 10, 2006 Author Share Posted July 10, 2006 Well, for this project I am dealing with some 220 HTML form fields whose values must find their way to a MySQL database. For the sake of my sanity, I am trying to keep the form fields the same name as the PHP variables and the MySQL fields. Also, time is the primary concern for this project, as are all of my projects at work, so generally while coding I like any amount of code that I can copy and paste, even if it's just field names. Quote Link to comment https://forums.phpfreaks.com/topic/14141-setting-vars-with-recurring-numbers-within-the-vars/#findComment-55514 Share on other sites More sharing options...
redarrow Posted July 10, 2006 Share Posted July 10, 2006 <?PHPfor ($counter = 1; $counter <= 200; $counter++){ $T[$counter] = 'T' . $counter; echo $T[$counter] . '<br>';}$t1=$T[1];$t2=$T[2];if ($t1){echo 'yes<br>';}else{echo 'no<br>';}if ($t2){echo 'yes<br>';}else{echo 'no<br>';}?> Quote Link to comment https://forums.phpfreaks.com/topic/14141-setting-vars-with-recurring-numbers-within-the-vars/#findComment-55521 Share on other sites More sharing options...
just-j Posted July 10, 2006 Share Posted July 10, 2006 for the $t1 = $t[1];ect....instead of typeing all those out couldnt you just do a loop. ive been studing php for about a week now so i couldnt really type an example but ill try ;)for ($i = 1; $i <=200; $i++){$t.$i = $t[.$i.];}looks like it would work.. i could be way off though.. HAH! Quote Link to comment https://forums.phpfreaks.com/topic/14141-setting-vars-with-recurring-numbers-within-the-vars/#findComment-55530 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2006 Share Posted July 10, 2006 Before posting code that "looks right," please test it first to see if it is right.Your code:[code]<?phpfor ($i = 1; $i <=200; $i++){$t.$i = $t[.$i.];}?>[/code]Is incorrect. The correct way of doing this is:[code]<?phpfor ($i = 1; $i <=200; $i++){${t.$i} = $t[$i];}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/14141-setting-vars-with-recurring-numbers-within-the-vars/#findComment-55540 Share on other sites More sharing options...
designationlocutus Posted July 10, 2006 Share Posted July 10, 2006 [code]${t.$i}[/code]Never come across that before. Does it allow you to concatenate two strings to create a variable name? Quote Link to comment https://forums.phpfreaks.com/topic/14141-setting-vars-with-recurring-numbers-within-the-vars/#findComment-55549 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.