millercj Posted December 10, 2008 Share Posted December 10, 2008 how do I create a dynamic variable name? I want to do something like for(i=0;i<2;i++){ $Lname.$i=$i; } where as the variable declared in the for loop would create 3 or so variables with sames like Lname0 Lname1 Lname2 Quote Link to comment https://forums.phpfreaks.com/topic/136399-variable-naming-help/ Share on other sites More sharing options...
Mchl Posted December 10, 2008 Share Posted December 10, 2008 I could tell you, but tell me first: Did you consider using arrays? Quote Link to comment https://forums.phpfreaks.com/topic/136399-variable-naming-help/#findComment-711701 Share on other sites More sharing options...
millercj Posted December 10, 2008 Author Share Posted December 10, 2008 Yes and no, i'm not sure if it would really be of benefit in this situation...let me know what you think: This is part of an ajax script that is adding values to database (First Name, Last Name, and Age for each record). The number of parameters is completely variable from 5-infinity I have a parameter that is always passed which I named $iterate which is a count of parameters being passed. Quote Link to comment https://forums.phpfreaks.com/topic/136399-variable-naming-help/#findComment-711711 Share on other sites More sharing options...
Mchl Posted December 10, 2008 Share Posted December 10, 2008 And it is very good idea to use arrays here Look for(i=0;i<2;i++){ $Lname[$i]=$i; } echo $Lname[0]; // 0 echo $Lname[1]; // 1 Quote Link to comment https://forums.phpfreaks.com/topic/136399-variable-naming-help/#findComment-711716 Share on other sites More sharing options...
Maq Posted December 10, 2008 Share Posted December 10, 2008 You need to use an array as Mchl said. Example: Then, however you submit, you can use for loops to iterate through the arrays! Quote Link to comment https://forums.phpfreaks.com/topic/136399-variable-naming-help/#findComment-711719 Share on other sites More sharing options...
PFMaBiSmAd Posted December 10, 2008 Share Posted December 10, 2008 And if you use a foreach() loop with an array, you don't even need to bother with your $iterate variable. Quote Link to comment https://forums.phpfreaks.com/topic/136399-variable-naming-help/#findComment-711742 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.