1
    $current_month  = date('M'); 
    for($m=1; $m<=$current_month; ++$m)
    {
        $monthNamelower=strtolower(date('M', mktime(0, 0, 0, $m, 1))).'';

            if ($jandata!='')
            {
              echo $jandata;
            }

        echo $data2jan;  
    }

can i replace the month jan with the variable $monthNamelower?
I tried something like this but not working

    if ('$'.$monthNamelower.'data'!='')
    {
            echo '$'.$monthNamelower.'data'; 

    }
    echo '$data2'.$monthNamelower; 
3
  • 1
    $monthNamelower .= 'data'; if (${$monthNamelower}) { } Commented Jul 5, 2017 at 6:43
  • @tilz0R, write it as answer. Commented Jul 5, 2017 at 6:44
  • It is quite difficult to understand your question, maybe it is better to edit it stackoverflow.com/help/how-to-ask. From where pops $jandata out? Maybe you would better use an array to store your data without using variables of variables (and if your data are already in place compact is your best friend!) Commented Jul 5, 2017 at 7:01

2 Answers 2

3

Yes, you can do it.

if (${$monthNamelower . 'data'} != '') {
    echo ${$monthNamelower . 'data'}; 
}

If your value stored in $monthNamelower is j, it will check for variable name jdata and will print it.

You can test this code directly to see effect:

$monthNamelower = 'j';
$jdata = 'January';
if (${$monthNamelower . 'data'} != '') {
    echo ${$monthNamelower . 'data'};  //Prints 'January'
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you ..clear explanation but not sure why its not working in my code ..my old variable is working..i'll try to figure it out first
1

You can do this by making your variable name as dynamic Just like this

$monthNamelower.='data';
if ($$t!='')
    {
echo $$t; 

}

For Example:-

$t='jan';
$jandate='good';
$t.='date';
echo $$t;

It will produce output like

enter image description here

I thing it will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.