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条)

报告相同问题?

悬赏问题

  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?