Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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