访问 PHP foreach 循环之外的变量

发布于 2024-11-15 17:49:43 字数 610 浏览 1 评论 0原文

在我正在处理的一些代码中,我注意到有几个变量是从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深居我梦 2024-11-22 17:49:43

我能想到这样做的唯一原因是寻找数组的末尾,以便可以将其最后一个成员存储在变量中。

您可以使用 end() 更清楚地做到这一点。

$row = end($items);

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 = end($items);
旧情别恋 2024-11-22 17:49:43

$row 将是数组中的最后一项,或者在到达其他代码时取消设置。

这就是你想要的吗?如果是这样,那么这本身并不是一个坏习惯。但您至少应该记录该行为是有意为之的,因为它并不直观。

更好的做法是这样的:

foreach ($foo as $bar) 
{
  // do something
}
$last_bar = isset($bar) ? $bar : null;

很明显,您打算用 $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:

foreach ($foo as $bar) 
{
  // do something
}
$last_bar = isset($bar) ? $bar : null;

There it's obvious that you mean to do something with $last_bar.

Another issue I've noticed is if a variable is required in several parts of a view file

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.

终难遇 2024-11-22 17:49:43

这是一个不好的做法。如果记录集的长度超过一项,那么您将循环遍历所有记录(即使您在循环中不执行任何操作),然后仅使用最后一条记录。我不使用 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, or end 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.

云之铃。 2024-11-22 17:49:43

我想说这是非常糟糕的做法 - 如果稍后(几周后)你遇到同一段代码并且你需要另一个完全不同的foreach,会发生什么实际 foreach 和表之间的记录类型? - 您的 $row 变量将具有完全不同的含义 - 当添加更多代码时,这段代码很容易受到副作用的影响。

另外,将来 PHP 很可能不再支持这种行为(这只是我的假设,因为正如您所提到的, $row 变量超出了您使用它的范围)。

作为一些一般原则:

  • 使用尽可能少的全局变量 - 它们很难跟踪、维护和确保正确的值
  • 尽可能最小化变量的范围,并且仅在非常明确的范围内使用它们
  • 避免边缘用例,即所谓的 < em>在其他语言中完全不自然的功能
  • 最后但并非最不重要的一点是,避免“如果代码很难编写就应该很难阅读”的原则 - 这个原则不会保证你的安全这份工作,只有同事的厌恶

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 actual foreach 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:

  • use as few as possible global variables - they are hard to track, maintain and assure correct values
  • minimize the scope of the variables as much as possible and use them only in very clear scopes
  • avoid marginal use cases and so called features that are totally unnatural in other languages
  • last, but not least, avoid the principle if code was hard to write it should be hard to read - this principle won't secure you the job, only the hate of your coworkers
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文