加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel foreach变量循环范围

发布时间:2020-12-14 19:45:14 所属栏目:大数据 来源:网络整理
导读:当我使用Laravel刀片文件foreach循环时,变量可以在foreach循环之后访问,而变量的范围应该只在循环内 @foreach($user-referral as $ref) tr td{{ $ref-referral_amount }}/td td{{ $ref-status }}/td /tr@endforeach $ref:这个变量可以在@endforeach之后的en
当我使用Laravel刀片文件foreach循环时,变量可以在foreach循环之后访问,而变量的范围应该只在循环内
@foreach($user->referral as $ref)
  <tr>
    <td>{{ $ref->referral_amount }}</td>
    <td>{{ $ref->status }}</td>
  </tr>
@endforeach

$ref:这个变量可以在@endforeach之后的endforeach循环之外访问

从 the foreach docs开始:

Warning

Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset()

因此,如果要销毁引用,请执行以下操作:

<?php unset($ref); ?>

要么:

@php unset($ref); @endphp

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读