PHP video tutorial 49 – PHP foreach loop in PHP



PHP foreach loop:
To traverse (or loop through) an array of elements, we use foreach loop

Syntax1:
foreach($arrayName as $variableName)
{
statement(s);
}
OR
foreach($arrayName as $variableName):
statement(s);
endforeach;

Syntax2:
foreach($arrayName as $key => $value)
{
statement(s);
}
OR
foreach($arrayName as $key => $value):
statement(s);
endforeach;

Example Code:
$names = array(“manju”,”ravi”,”rama”);

foreach($names as $value)
{
echo $value,”<br/>”;
}
foreach($names as $value):
echo $value,”<br/>”;
endforeach;

foreach($names as $key => $value)
{
echo $key, ” “, $value,”<br/>”;
}
foreach($names as $key => $value):
echo $key, ” “, $value,”<br/>”;
endforeach;

$names2 = array(“first”=>”manju”,”second”=>”ravi”,”third”=>”rama”);

foreach($names2 as $value)
{
echo $value,”<br/>”;
}
foreach($names2 as $key => $value)
{
echo $key, ” “, $value,”<br/>”;
}

=========================================

Follow the link for next video:
PHP video tutorial 50 – PHP break statement in PHP

Follow the link for previous video:
PHP video tutorial 48 – PHP do while loop in PHP

=========================================

Nguồn: https://svdpch.org/

Xem thêm bài viết khác: https://svdpch.org/cong-nghe/

4 thoughts on “PHP video tutorial 49 – PHP foreach loop in PHP”

  1. SUBSCRIBE, SHARE & SUPPORT:

    https://www.youtube.com/chidrestechtutorials

    VISIT & LEARN AT FREE OF COST:

    ​https://www.chidrestechtutorials.com

    Reply
  2. $employees= array(

    array("empId: "=> "013", "empName: "=>"vayu", "empSal: "=>100000),

    array("empId: "=> "014", "empName: "=>"surya", "empSal: "=>200000),

    array("empId: "=> "015", "empName: "=>"varun", "empSal: "=>100000)

    );

    how to display multiple arrays using foreach loop

    Reply

Leave a Comment