“如” PHP 中的运算符

发布于 2024-11-02 11:08:51 字数 114 浏览 2 评论 0原文

在 PHP 中,运算符 'as' 的作用是什么?例如,

foreach ( $this->Example as $Example )

谢谢。

In PHP, what does the operator 'as' do? For example,

foreach ( $this->Example as $Example )

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

千柳 2024-11-09 11:08:51

举个例子:

foreach ($array as $value) {}

这意味着

foreach 元素($array)定义为
$值...

换句话说,这意味着数组的元素将在循环内以as $value 的形式检索。
请注意,您还可以指定此语法:

foreach ($array as $key => $value) {}

在这两种方式中,变量 $key$value 都将存在于 foreach 中,并且对应于数组的“当前”元素。

Let's take an example:

foreach ($array as $value) {}

This means

foreach element (of $array) defined as
$value...

In other words this means that the elements of the array will be retrieved inside the loop as $value.
Notice that you can specify also this syntax:

foreach ($array as $key => $value) {}

In both ways the vars $key and $value will exists inside the foreach and will correspond to the 'current' element of the array.

猫卆 2024-11-09 11:08:51

在 foreach 循环的每次迭代中,$Example 被分配给循环中的当前元素。

On each iteration of the foreach loop, $Example is assigned to the current element in the loop.

酒几许 2024-11-09 11:08:51

只是为了扩展现有答案。 as 关键字有不止一种用途。除了显而易见的事情之外:

foreach ($dictionary as $key => $value) {}

它还用于为导入的资源添加别名:

<?php
namespace foo;
use Full\Class\Path as MyAlias;
new MyAlias();

这很有用,例如作为 AVeryLongClassNameThatRendersYourCodeUnreadable 的简写,或者在扩展第三方代码时用您的实现替换对象。

Just to expand on existing answers. There's more than one use of as keyword. Besides the obvious:

foreach ($dictionary as $key => $value) {}

It's also used to alias imported resources:

<?php
namespace foo;
use Full\Class\Path as MyAlias;
new MyAlias();

Which is useful, e.g. as a shorthand for AVeryLongClassNameThatRendersYourCodeUnreadable or to substitute an object with your implementation when extending third party code.

↘人皮目录ツ 2024-11-09 11:08:51

它仅与 foreach 一起使用,并定义表示数组中当前项目的变量(或键/值变量对)。

It's only used with foreach and it defines the variable (or key/value variable pair) that represents the current item in the array.

随遇而安 2024-11-09 11:08:51

这是来自 http://php.net/manual/en/control-structs 的信息.foreach.php

foreach (array_expression as $value)
    statement

第一个表单循环遍历由 array_expression 给出的数组。

在每个循环中,当前元素的值被分配给 $value 并且内部数组指针前进一位(因此在下一个循环中,您将查看下一个元素)。

here is info from http://php.net/manual/en/control-structures.foreach.php

foreach (array_expression as $value)
    statement

The first form loops over the array given by array_expression.

On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element).

執念 2024-11-09 11:08:51

获取数组中的每个条目,即 $this->Example 并将其放入 $Example

中的更多详细信息 http://php.net/manual/en/control-structs.foreach.php

Takes each entries in the array ie, $this->Example and puts it in $Example

more details in http://php.net/manual/en/control-structures.foreach.php

小耗子 2024-11-09 11:08:51

它用在 foreach 构造中进行迭代通过一个数组。 foreach 块中的代码将为数组中的每个元素运行(在本例中为 $this->Example)。每次运行时,as 后面的变量(在本例中为 $Example)将被设置为数组中当前元素的值。

It's used in a foreach construct to iterate through an array. The code in the foreach block will be run for each element in the array (in this example, $this->Example). Every time it is run, the variable after as (in this example, $Example) will be set to the value of the current element in the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文