Home  >  Article  >  Backend Development  >  foreach function in php_PHP tutorial

foreach function in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:12:111047browse

Foreach 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 it to variables of other data types, or uninitialized variables, an error message will be issued.
has two syntaxes:
[php]
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
Example 1:
[php]
$arr=array(1,2,3,4);
foreach($arr as $value){
echo $value."
";
}
?>
$arr=array(1,2,3,4);
foreach($arr as $value){
echo $value."
";
}
?>
Example 2:
[php]
$arr=array(1,2,3,4);
foreach($arr as $key=>$value){
echo $key."=====>".$value."
";
}
?>
$arr=array(1,2,3,4);
foreach($arr as $key=>$value){
echo $key."=====>".$value."
";
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477218.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 it 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