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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测