
For Loop with Numeric Array:-
Numeric Array uses in For Loop for Declaration and Initialization as numeric index values are store and access.
ex:-
<?php | |
$array_name [0] = "value"; | |
$array_name [1] = "value1"; | |
$array_name [2] = "value2"; | |
$array_name [3] = "value3"; | |
for ($a = 0; $a <= 3 ; ++$a) | |
{ | |
echo $array_name[$a], "<br>"; | |
} | |
//second example | |
$array_name = array ("name", "name1", "name2", "name3"); | |
for ($a = 0; $a <= 3; ++$a) | |
{ | |
echo $array_name[$a], "<br>"; | |
} | |
?> |
For Loop with Associative Array:-
The associative array used in For Loop with strings as an index. Associative array stores element values associated with key or index ([]= this is index or key) values.
ex:-
<?php | |
$name = array ("value" => 500, "value1" => 400, "value2" => 300, "value3" => 200, "value4" => 100); | |
$keys = array_keys($name); | |
for ($a = 0; $a< count($keys); ++$a) // Before check condition then executed | |
{ | |
echo $keys[$a], "<br>"; | |
} | |
?> |
Foreach Loop with Numeric array:-
Foreach Loop works only on arrays. It is used as each index, key or value pair in an array.
ex:-
<?php | |
$name [0] = "value"; | |
$name [1] = "value1"; | |
$name [2] = "value2"; | |
$name [3] = "value3"; | |
foreach($name as $val){ | |
echo $val . "<br>"; | |
} | |
//second Type | |
$name = array ("name", "name1", "name2", "name3", "name4"); | |
foreach ($name as $var){ | |
echo $var , "<br>"; | |
} | |
?> |
Foreach Loop with Associative array:-
ex:-
<?php | |
$name = array ("name"=> 100, "name1" => 200, "name2" => 300, "name3" => 400, "name4" =>500); | |
foreach ($name as $var) | |
{ | |
echo $var , "<br>"; | |
} | |
?> |
While Loop with Numeric Array:-
<?php | |
$value = array ("name", "name1", "name2", "name3", "name4"); | |
$a = 0; | |
while($a < count ($value)) | |
{ | |
echo "this is numeric while loop <br>"; | |
$a++; | |
} | |
?> |
Iβm a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND