“如” PHP 中的运算符
在 PHP 中,运算符 'as' 的作用是什么?例如,
foreach ( $this->Example as $Example )
谢谢。
In PHP, what does the operator 'as' do? For example,
foreach ( $this->Example as $Example )
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
举个例子:
这意味着
换句话说,这意味着数组的元素将在循环内以
as
$value
的形式检索。请注意,您还可以指定此语法:
在这两种方式中,变量
$key
和$value
都将存在于foreach
中,并且对应于数组的“当前”元素。Let's take an example:
This means
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:
In both ways the vars
$key
and$value
will exists inside theforeach
and will correspond to the 'current' element of the array.在 foreach 循环的每次迭代中,
$Example
被分配给循环中的当前元素。On each iteration of the foreach loop,
$Example
is assigned to the current element in the loop.只是为了扩展现有答案。
as
关键字有不止一种用途。除了显而易见的事情之外:它还用于为导入的资源添加别名:
这很有用,例如作为
AVeryLongClassNameThatRendersYourCodeUnreadable
的简写,或者在扩展第三方代码时用您的实现替换对象。Just to expand on existing answers. There's more than one use of
as
keyword. Besides the obvious:It's also used to alias imported resources:
Which is useful, e.g. as a shorthand for
AVeryLongClassNameThatRendersYourCodeUnreadable
or to substitute an object with your implementation when extending third party code.它仅与 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.这是来自 http://php.net/manual/en/control-structs 的信息.foreach.php
第一个表单循环遍历由 array_expression 给出的数组。
在每个循环中,当前元素的值被分配给 $value 并且内部数组指针前进一位(因此在下一个循环中,您将查看下一个元素)。
here is info from http://php.net/manual/en/control-structures.foreach.php
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).
获取数组中的每个条目,即 $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
它用在
foreach
构造中进行迭代通过一个数组。foreach
块中的代码将为数组中的每个元素运行(在本例中为$this->Example
)。每次运行时,as
后面的变量(在本例中为$Example
)将被设置为数组中当前元素的值。It's used in a
foreach
construct to iterate through an array. The code in theforeach
block will be run for each element in the array (in this example,$this->Example
). Every time it is run, the variable afteras
(in this example,$Example
) will be set to the value of the current element in the array.