如何在php中包含变量到函数名

时间:2017-03-11 13:38:11

标签: php

我想将php变量top包含到具有参数

的函数名中
$i

在上面的代码中我想将函数名称设为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); } ,函数调用为function draw_calendar$i($month,$year,$per_day_chu){

有没有办法实现这一目标。我不知道如何设置它。三江源。

2 个答案:

答案 0 :(得分:3)

你应该将你的函数放在循环之外,然后将调用放入循环中。

答案 1 :(得分:2)

我认为你实际上需要这样做: -

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

注意: -

内部循环中的函数代码是错误的做法。

同时检查$per_day_chu圈内每次for是否可用,并且每次都有正确的值。