loop in php in javascript

To loop in PHP and JavaScript, you can use for loop, foreach loop, while loop, and do-while loop.

PHP loop example:

main.php
<?php
$colors = array("red", "green", "blue");

//FOR LOOP
for($i=0;$i<count($colors);$i++){
    echo $colors[$i]."<br>";
}

//FOREACH LOOP
foreach($colors as $value){
    echo $value."<br>";
}

//WHILE LOOP
$i = 0;
while($i<count($colors)){
    echo $colors[$i]."<br>";
    $i++;
}

//DO-WHILE LOOP
$i = 0;
do{
    echo $colors[$i]."<br>";
    $i++;
}while($i<count($colors));

?>
382 chars
29 lines

JavaScript loop example:

index.tsx
var colors = ["red", "green", "blue"];

//FOR LOOP
for (var i = 0; i < colors.length; i++) {
    console.log(colors[i]);
}

//FOREACH LOOP (ForIn Loop)
for (var i in colors) {
    console.log(colors[i]);
}

//FOREACH LOOP (Functional)
colors.forEach(function (value) {
    console.log(value);
});

//WHILE LOOP
var i = 0;
while (i < colors.length) {
    console.log(colors[i]);
    i++;
}

//DO-WHILE LOOP
var i = 0;
do {
    console.log(colors[i]);
    i++;
} while (i < colors.length);
488 chars
31 lines

gistlibby LogSnag