Home  >  Article  >  Backend Development  >  In-depth analysis of the foreach function in php_PHP tutorial

In-depth analysis of the foreach function in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:12:16785browse

Foreach function (PHP4/PHP5)
foreach The syntax structure provides a simple way to traverse an array.
foreach can only be applied to arrays and objects. If you try to apply to variables of other data types, or uninitialized variables, an error message will be issued.

has two syntaxes:

Copy the code The code is as follows:

foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement

Example 1:
Copy code The code is as follows:

$arr=array(1,2,3, 4);
foreach($arr as $value){
echo $value."
";
}
?>

Example 2:
Copy code The code is as follows:

$arr =array(1,2,3,4);
foreach($arr as $key=>$value){
echo $key."=====>".$value."
";
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313669.htmlTechArticleForeach function (PHP4/PHP5) The foreach syntax structure provides a simple way to traverse an array. foreach can only be applied to arrays and objects, if you try to apply to variables of other data types...
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