访问 PHP foreach 循环之外的变量
在我正在处理的一些代码中,我注意到有几个变量是从 foreach
循环外部访问的。
这可以在这个特定应用程序的 codeigniter 视图文件中看到。
例如,一个视图文件:
<?php
foreach ($items as $row):
endforeach;
// HTML / PHP code...
<td>
<?php echo form_checkbox('option_1','1', FALSE); ?>
<?php echo form_hidden('weight_unit', $row->weight_unit); ?>
</td>
// etc...
这有效(即没有错误),但我想知道这是否被认为是一种不好的做法,如果是,为什么? (范围等)
有人对此有意见吗?变量应该只在相应的循环内调用吗?
我注意到的另一个问题是,视图文件的多个部分是否需要一个变量:我应该重构为具有多个循环,还是应该有一个 foreach / endforeach
以及该变量的开始/结束文件。
非常感谢任何建议。谢谢。
In some code I'm working on I have noticed that several variables are being accessed from outside of foreach
loops.
This is seen in codeigniter view files of this particular app.
For example, a view file:
<?php
foreach ($items as $row):
endforeach;
// HTML / PHP code...
<td>
<?php echo form_checkbox('option_1','1', FALSE); ?>
<?php echo form_hidden('weight_unit', $row->weight_unit); ?>
</td>
// etc...
This works (ie, no errors) but I wonder if this would be considered a bad practice and if so, why? (scope, etc)
Does anyone have an opinion on this and should variables only be called inside their corresponding loops?
Another issue I've noticed is if a variable is required in several parts of a view file: should I refactor to have multiple loops or should there be a single foreach / endforeach
and the begin / end of the file.
Any suggestions are much appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我能想到这样做的唯一原因是寻找数组的末尾,以便可以将其最后一个成员存储在变量中。
您可以使用
end()
更清楚地做到这一点。The only reason I could think of doing that is to seek to the end of the array so you can store its last member in a variable.
You can do this clearer with
end()
.$row
将是数组中的最后一项,或者在到达其他代码时取消设置。这就是你想要的吗?如果是这样,那么这本身并不是一个坏习惯。但您至少应该记录该行为是有意为之的,因为它并不直观。
更好的做法是这样的:
很明显,您打算用
$last_bar
做一些事情。如果您必须在视图中的不同位置迭代循环,那么没关系。
但是,如果您只需要某个数组深处的某条信息,那么您应该将其粘贴到一个易于访问的变量中并使用它。
$row
will be the last item in the array, or unset, when it reaches your other code.Is that want you want? If so, then it's not bad practice, per se. But you should at least document that the behavior is intended, because it's not intuitive.
Better practice is something like:
There it's obvious that you mean to do something with
$last_bar
.If you must iterate over your loop in different places in your view, then that's okay.
But if you just need a certain piece of information deep inside some array, then you should stick it into an easily accessible variable and use that instead.
这是一个不好的做法。如果记录集的长度超过一项,那么您将循环遍历所有记录(即使您在循环中不执行任何操作),然后仅使用最后一条记录。我不使用 CI,但如果这是一个记录集对象,则应该有某种方式来访问 RS 中的第一个/最后一个记录或任何其他索引位置。如果您在寻找特定记录,则应该使用它们。如果它只是一个数组,如果您不需要保持数组完整,则可以使用
array_pop
,如果需要,则可以使用end
。此外,在长集的相同上下文中,如果它实际上不是最后一条记录,那么如果记录集长度发生变化,则可能会产生不可预见的后果。
This is a bad practice. If the recordset is more than one item long then you are looping through all the records (even though you do nothing in the loop) and then just using the last record. I dont use CI but if this is a recordset object there ought to be someway to access the first/last record or any other index position in the RS. You should use those if youre after a particular record. If its just an array you could use
array_pop
if you dont need to keep the array intact, orend
if you do.Additionally, in the same context of a lengthy set if its not really the last record your after this could have unforseen consequences if the recordset length changes.
我想说这是非常糟糕的做法 - 如果稍后(几周后)你遇到同一段代码并且你需要另一个完全不同的
foreach
,会发生什么实际foreach
和表之间的记录类型? - 您的$row
变量将具有完全不同的含义 - 当添加更多代码时,这段代码很容易受到副作用的影响。另外,将来 PHP 很可能不再支持这种行为(这只是我的假设,因为正如您所提到的, $row 变量超出了您使用它的范围)。
作为一些一般原则:
I'd say this is really bad practice - what happens if later (in a few weeks) you come across the same piece of code and you need another
foreach
on totally different record types between the actualforeach
and your table? - your$row
variable will then have a totally different meaning - this piece of code is very susceptible to side effects when more code is added.Also, there is a high possibility that in the future this behavior will not be supported by PHP anymore (this is just an assumption of mine, because, as you mentioned, the $row variable is out of scope where you are using it).
As some general principles: