gtroop
gtroop

Reputation: 293

combining two php variables

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

Upvotes: 1

Views: 47

Answers (2)

Bibhudatta Sahoo
Bibhudatta Sahoo

Reputation: 4894

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.

Upvotes: 1

unalignedmemoryaccess
unalignedmemoryaccess

Reputation: 7441

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'
}

Upvotes: 3

Related Questions