12dstring Posted August 30, 2007 Share Posted August 30, 2007 I have a list of 12 variables, like so $d1=31; $d2-28; $d3=31; $d4=30 .... $d12=31; and another one called $month which is a numerical value from 1-12 (calculated seperately) What I need is a variable, $dmonth, which is the value of $d"$month" For example if $month=1 then $dmonth=$d1 (=31) and likewise if $month=12, then $dmonth=$d12 I can't for the life of me figure out how to code a variable as part of a defined variable, so any help would be much appreciated. Thanks in advance, Dave Link to comment https://forums.phpfreaks.com/topic/67314-solved-variable-within-a-variable/ Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 $dmonth = $d.$month; Link to comment https://forums.phpfreaks.com/topic/67314-solved-variable-within-a-variable/#findComment-337721 Share on other sites More sharing options...
akitchin Posted August 30, 2007 Share Posted August 30, 2007 not sure jesirose understood your question: $varname = 'd'.$month; echo 'days in this month: '.$$varname; it might be smarter to use an array and simply define the indeces by month: $d[1] = 31; $d[2] = 28; ... then to access the number of days, all you have to do is: echo 'days in this month: '.$d[$month]; Link to comment https://forums.phpfreaks.com/topic/67314-solved-variable-within-a-variable/#findComment-337722 Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 When I read it the first time I read it the way you saw, then I read it again and thought it was different. Gah. *headdesk* I had it right Link to comment https://forums.phpfreaks.com/topic/67314-solved-variable-within-a-variable/#findComment-337723 Share on other sites More sharing options...
12dstring Posted August 30, 2007 Author Share Posted August 30, 2007 The array route sorted it a treat. Thanks for the super-quick help guys Link to comment https://forums.phpfreaks.com/topic/67314-solved-variable-within-a-variable/#findComment-337726 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.