doufei0933 2015-09-28 11:36
浏览 49
已采纳

PHP - 是在foreach循环本地的foreach循环中定义的变量吗?

Let's say I have this foreach:

foreach ($items as $item) {
    $name = $item->name;
    $number = $item->number;
}

and I want to use them else where on the same page - say below the containing div like this:

<div>
    <!-- where the php foreach would go -->
</div>

<div>
 <?= $name ?>
</div>

would this break the page? or are the variables local to anywhere after the foreach loop?

  • 写回答

1条回答 默认 最新

  • dongmoxin7111 2015-09-28 11:54
    关注

    Unlike Java and C++, variables declared inside blocks such as loops or if's, will also be recognized and accessible outside of the block. As written in PHP Manual.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 Ubuntu开机显示器只显示kernel,是没操作系统(相关搜索:显卡驱动)
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 在rhel8中安装qemu-kvm时遇到“cannot initialize crypto:unable to initialize gcrypt“报错”
  • ¥15 arbotix没有/cmd_vel话题
  • ¥20 找能定制Python脚本的
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀