duanmao1319 2017-04-09 01:31
浏览 36
已采纳

如何在同一页面上多次使用某个功能?

Say this piece of code:

 <?php while($user=mysqli_fetch_array($resultuser)){ ?>
 <?php 

   function my_function($variable) {
      //do something here...
    }
 ?>
<?php };?>

This will obviously return this error

Cannot redeclare my_function() previously declared

So what if someone needs to use the same function multiple times across the same page? Is there a way to generate random function() names? Any idea how to get around this? Thanks.

EDIT WITH ACTUAL CODE

    <?php while($deposit26=mysqli_fetch_array($resultdeposit26)){ ?>
<td  data-th="Hours">
    <?php 
        $week1hours = $deposit26['hours_worked'];
        $week2hours = $deposit26['hours_worked_wk2'];
        function time_to_decimal($time) {
        $timeArr = explode(':', $time);
        $decTime = ($timeArr[0] + ($timeArr[1]/60) + ($timeArr[2]/3600));
        return $decTime;
        }
        $groupd26hours = time_to_decimal($week1hours) + time_to_decimal($week2hours);
        echo round($groupd26hours, 2);
     ?>
</td>
  <?php };?>
  • 写回答

3条回答 默认 最新

  • doujianwei8217 2017-04-09 01:41
    关注
    <?php 
    // Earlier in the file or included with include or require
    function time_to_decimal($time) {
            $timeArr = explode(':', $time);
            $decTime = ($timeArr[0] + ($timeArr[1]/60) + ($timeArr[2]/3600));
            return $decTime;
    } ?>
    

    ...

        <?php while($deposit26=mysqli_fetch_array($resultdeposit26)) : ?>
        <td  data-th="Hours">
        <?php 
            $week1hours = $deposit26['hours_worked'];
            $week2hours = $deposit26['hours_worked_wk2'];
            $groupd26hours = time_to_decimal($week1hours) +   time_to_decimal($week2hours);
            echo round($groupd26hours, 2); ?>
        </td>
        <?php endwhile ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存