Jithin Varghese Jithin Varghese - 9 months ago 33
PHP Question

how to include variable to function name in php

I want to include php variable

$i
to a function name having parameters

for($i=1; $i<=50; $i++) {
$currentMonth = date("F");

/* draws a calendar */
function draw_calendar($month,$year,$per_day_chu){
$currentDayOfMonth = date("j");
/* draw table */
$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

/* table headings */
$headings = array('Su','Mo','Tu','Wed','Th','Fr','Sa');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();
/* row for week one */
$calendar.= '<tr class="calendar-row">';
return $calendar;
}

echo $currentMonth.", ".date("Y");
echo draw_calendar(date("m"), date("Y"), $per_day_chu);
}


in the above code I want to make the function name as
function draw_calendar$i($month,$year,$per_day_chu){
and function call as
echo draw_calendar(date("m"), date("Y"), $per_day_chu);


Is there any way to achieve this. I have no idea how to set this up. Thankyou.

Answer Source

I think you need to do this actually:-

for($i=1; $i<=50; $i++) {
  $currentMonth = date("F");
  echo $currentMonth.", ".date("Y");
  echo draw_calendar(date("m"), date("Y"), $per_day_chu);
}

function draw_calendar($month,$year,$per_day_chu){
    $currentDayOfMonth = date("j");

    $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';


    $headings = array('Su','Mo','Tu','Wed','Th','Fr','Sa');
    $calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';


    $running_day = date('w',mktime(0,0,0,$month,1,$year));
    $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
    $days_in_this_week = 1;
    $day_counter = 0;
    $dates_array = array();

    $calendar.= '<tr class="calendar-row">';
    return $calendar;
}

Note:-

function code inside loop is wrong practice.

Also check that $per_day_chu will be available every-time inside your for loop and have some correct value each time.