如何在每次迭代后销毁循环中定义的变量?
How to destroy variables defined in loop after each iteration?
在PHP中,循环中定义的变量不是循环的局部变量。有什么方法可以 destroying/un-setting 在每次迭代后声明每个变量吗?
如果我在 foreach
循环中的 if
语句中声明一个变量,问题就来了。这个变量不一定在每次迭代中都声明,所以当我想要它被销毁时,它可能会徘徊并具有与上次相同的值!
具体来说,这是我的(简化的)代码。它解析 events.xml
文件,其中包含 <event>
个元素,这些元素都具有 <startDate>
个子元素,可能有也可能没有 <endDate>
个子元素,然后形成 html 循环遍历所有事件后显示事件。
<html>
<?php
$events = simplexml_load_file("events.xml");
foreach ($events as $value):
// get the start date of the current event from the xml file
$startDate = strtotime($value->startDate);
// get the end date if it exists (it might not)
$endDate = strtotime($value->endDate);
// get day of the week from the start date
$startDay = date("D", $startDate);
if ($endDate) {
$endDay = date("D", $endDate);
$dash = "-";
}
?>
<div class="event"> <!-- this is still in the foreach loop -->
<!-- insert the start day (always) followed by a dash and end day (if they exist) -->
<?php echo $startDay, $dash, $endDay; ?>
</div>
<?php endforeach; ?>
</html>
问题是,如果在 events.xml
文件中,我有一个有结束日期的事件,后面跟着一个没有结束日期的事件,后者的 div 将有结束日期从前者开始(因为 $endDay
变量没有被取消设置),当我根本不希望它有结束日期时。 (如果 xml 文件顶部的事件没有结束日期,则其 div 将没有结束日期。)
有趣的是,对于没有结束日期的事件,$endDate
变量似乎确实在这一行被破坏:$endDate = strtotime($value->endDate);
,大概是因为它试图从 xml 什么也没找到。但是,如果我将 $endDay
声明放在 if
语句之外,那么默认情况下它将转到 01
,这是我不想要的。
您必须自行重置变量。它们在当前范围内都是可见的,循环或条件没有自己的 var 范围。
在您的情况下,您必须在每次迭代时重置 $dash
和 $endDay
- 即
foreach ($events as $value):
$dash = null;
$endDay = null;
//...
?>
$startDate
和 $endDate
在每次迭代中被覆盖。如果 $value->start/endDate
未设置或无效日期,它们将变为 false
作为值。
在PHP中,循环中定义的变量不是循环的局部变量。有什么方法可以 destroying/un-setting 在每次迭代后声明每个变量吗?
如果我在 foreach
循环中的 if
语句中声明一个变量,问题就来了。这个变量不一定在每次迭代中都声明,所以当我想要它被销毁时,它可能会徘徊并具有与上次相同的值!
具体来说,这是我的(简化的)代码。它解析 events.xml
文件,其中包含 <event>
个元素,这些元素都具有 <startDate>
个子元素,可能有也可能没有 <endDate>
个子元素,然后形成 html 循环遍历所有事件后显示事件。
<html>
<?php
$events = simplexml_load_file("events.xml");
foreach ($events as $value):
// get the start date of the current event from the xml file
$startDate = strtotime($value->startDate);
// get the end date if it exists (it might not)
$endDate = strtotime($value->endDate);
// get day of the week from the start date
$startDay = date("D", $startDate);
if ($endDate) {
$endDay = date("D", $endDate);
$dash = "-";
}
?>
<div class="event"> <!-- this is still in the foreach loop -->
<!-- insert the start day (always) followed by a dash and end day (if they exist) -->
<?php echo $startDay, $dash, $endDay; ?>
</div>
<?php endforeach; ?>
</html>
问题是,如果在 events.xml
文件中,我有一个有结束日期的事件,后面跟着一个没有结束日期的事件,后者的 div 将有结束日期从前者开始(因为 $endDay
变量没有被取消设置),当我根本不希望它有结束日期时。 (如果 xml 文件顶部的事件没有结束日期,则其 div 将没有结束日期。)
有趣的是,对于没有结束日期的事件,$endDate
变量似乎确实在这一行被破坏:$endDate = strtotime($value->endDate);
,大概是因为它试图从 xml 什么也没找到。但是,如果我将 $endDay
声明放在 if
语句之外,那么默认情况下它将转到 01
,这是我不想要的。
您必须自行重置变量。它们在当前范围内都是可见的,循环或条件没有自己的 var 范围。
在您的情况下,您必须在每次迭代时重置 $dash
和 $endDay
- 即
foreach ($events as $value):
$dash = null;
$endDay = null;
//...
?>
$startDate
和 $endDate
在每次迭代中被覆盖。如果 $value->start/endDate
未设置或无效日期,它们将变为 false
作为值。