Home  >  Article  >  Backend Development  >  What does as mean in php

What does as mean in php

WBOY
WBOYOriginal
2022-05-07 14:59:404265browse

In PHP, as means to use a substitute variable to represent the elements in the traversed array. as is used in the foreach statement. It is a method of traversing the array. The syntax is "foreach (array_expression as $value){statement}".

What does as mean in php

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer

What does as mean in php

as is used in the foreach statement, which means that the elements in the traversed array are represented by a substitute variable.

PHP 4 introduced the foreach structure, which is a convenient way to traverse an array. foreach can only be used with arrays, and an error will occur when trying to use it with other data types or an uninitialized variable.

There are two syntaxes, the second is a minor but useful extension of the first.

foreach (array_expression as $value){ 语句 }
foreach (array_expression as $key => $value) { 语句 }

$value,$key is equivalent to a loop variable, such as $i in a for loop. You can choose a name at will, and it will be equal to the value and subscript of each element of the array in sequence during the loop.

The first format iterates over the given array_expression array. Each time through the loop, the value of the current cell is assigned to $value and the pointer inside the array is moved forward one step (so the next cell will be obtained in the next loop).

The second format does the same thing, except that the key name of the current unit will also be assigned to the variable $key in each loop.

The example is as follows:

$a = array('Tom','Mary','Peter','Jack');

We use the first foreach method to output.

foreach ($a as $value) {
  echo $value."<br/>";
}

The final result is:

What does as mean in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What does as mean in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn