php PHP中的“as”运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5720260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'as' operator in PHP
提问by Randomblue
In PHP, what does the operator 'as' do? For example,
在 PHP 中,运算符“as”有什么作用?例如,
foreach ( $this->Example as $Example )
Thanks.
谢谢。
回答by Shoe
Let's take an example:
让我们举个例子:
foreach ($array as $value) {}
This means
这意味着
foreach element (of
$array
) defined as$value
...
foreach 元素 (of
$array
) 定义为$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:
换句话说,这意味着将在循环内检索数组的元素as
$value
。请注意,您还可以指定以下语法:
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.
在这两种方式中,vars$key
和$value
will 都存在于foreach
数组中,并且将对应于数组的“当前”元素。
回答by Ry-
It's only used with foreach
and it defines the variable (or key/value variable pair) that represents the current item in the array.
它仅用于foreach
和它定义表示数组中当前项的变量(或键/值变量对)。
回答by Mike Lewis
On each iteration of the foreach loop, $Example
is assigned to the current element in the loop.
在 foreach 循环的每次迭代中,$Example
分配给循环中的当前元素。
回答by Sergej Jevsejev
here is info from http://php.net/manual/en/control-structures.foreach.php
这是来自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.
第一种形式循环遍历由 array_expression 给出的数组。
On each loop, the value of the current element is assigned to $valueand the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element).
在每个循环中,当前元素的值被分配给 $value并且内部数组指针前进一个(因此在下一个循环中,您将查看下一个元素)。
回答by cprn
Just to expand on existing answers. There's more than one use of as
keyword. Besides the obvious:
只是为了扩展现有的答案。as
关键字的用法不止一种。除了显而易见的:
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.
这很有用,例如,AVeryLongClassNameThatRendersYourCodeUnreadable
在扩展第三方代码时,作为一个对象的简写或用您的实现替换一个对象。
回答by lonesomeday
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.
它在foreach
构造中用于遍历数组。foreach
块中的代码将针对数组中的每个元素(在本例中为$this->Example
)运行。每次运行时,变量 after as
(在本例中为$Example
)将设置为数组中当前元素的值。
回答by sv_in
Takes each entries in the array ie, $this->Example and puts it in $Example
获取数组中的每个条目,即 $this->Example 并将其放入 $Example
more details in http://php.net/manual/en/control-structures.foreach.php
更多细节在http://php.net/manual/en/control-structures.foreach.php