Home >> PHP >> Foreach loop in PHP

Foreach loop in PHP

The foreach provides to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type. There are two syntaxes:

Syntax

foreach (array as $value){
     // Code to be executed;
}

foreach (array as $key => $value){
     // Code to be executed;
}

Example

$array=array(1,2,3,4);
foreach($array as $value){
    echo $value;
}

Post Your Comment

Next Questions
Operators
Arithmatic operators
Assignment operators
Increment / Decrement Operators
Comparision Operators
Logical operators
Array Operators
String
addcslashes() function
addslashes() function
chunk_split() function
crc32() function
explode() function
implode() function
join() function
ltrim() function
md5() function
metaphone() function
money_format() function
nl2br() function
number_format() function
ord() function
parse_str() function
printf() function
quotemeta() function

Copyright ©2022 coderraj.com. All Rights Reserved.