问题描述
在PHP中,循环中定义的变量不是该循环的局部变量.有没有办法破坏/取消设置每次迭代后声明的每个变量?
In PHP, variables defined in a loop are not local to the loop. Is there any way of destroying/un-setting every variable declared after each iteration?
如果我在 foreach
循环的 if
语句中声明变量,麻烦就来了.不必在每次迭代中都声明此变量,所以当我希望将其销毁时,它有可能会徘徊并具有与上次相同的值!
The trouble comes if I declare a variable in an if
statement in a foreach
loop. This variable is not necessarily declared on each iteration, so there's the possibility of it hanging around and having the same value as last time, when I want it destroyed!
具体来说,这是我的(简化)代码.它解析并 .events.xml 文件,其中包含< event>
元素,这些元素都具有< startDate>
子元素,并且可能或可能没有< endDate>
子元素,然后在将事件全部循环之后形成显示事件的html.
Specifically, here is my (simplified) code. It parses and events.xml
file that contains <event>
elements that all have a <startDate>
child element, and may or may not have an <endDate>
child element, and then forms html showing the events after looping over them all.
<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将没有结束日期.)
The problem being, if in the events.xml
file, I have an event with an end date followed by an event without an end date, the div for the latter will have the end day from the former (because the $endDay
variable was not unset), when I don't want it to have an end date at all. (If there is an event without an end date at the top of the xml file, its div won't have an end date.)
有趣的是,对于没有结束日期的事件, $ endDate
变量似乎在以下行被破坏: $ endDate = strtotime($ value-> endDate);
,大概是因为它试图从xml读取它却什么也没找到.但是,如果我将 $ endDay
声明放在 if
语句之外,则默认情况下它将进入 01
,这是我不希望的.
Interestingly, for an event with no end date, the $endDate
variable does seem to get destroyed at this line: $endDate = strtotime($value->endDate);
, presumably because it tries to read it from the xml and finds nothing. But if I put the $endDay
declaration outside of the if
statement, then it would go to 01
by default, which I don't want.
推荐答案
您必须自行重置var.它们在当前作用域中都是可见的,循环或条件没有自己的var作用域.
You have to reset the vars on your own. They are all visible in the current scope and loops or conditions dont have their own var scope.
在您的情况下,您必须在每次迭代中重置 $ dash
和 $ endDay
-即
In your case you have to reset $dash
and $endDay
on each iteration - i.e.
foreach ($events as $value):
$dash = null;
$endDay = null;
//...
?>
$ startDate
和 $ endDate
在每次迭代中均被覆盖.如果未设置 $ value-> start/endDate
或无效的日期,则它们将成为 false
作为值.
$startDate
and $endDate
are overwritten in each iteration. If $value->start/endDate
is unset or an invalid date, they will become false
as value.
这篇关于每次迭代后如何销毁循环中定义的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!