Jump to content

[SOLVED] Variable within a variable


12dstring

Recommended Posts

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

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];

Archived

This topic is now archived and is closed to further replies.

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