foreach function in php_PHP tutorial

WBOY
Release: 2016-07-14 10:12:11
Original
1046 people have browsed it

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,...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!