Question

How to use for loop in phtml page i want to use the for loop in phtml page to complete my functionality

<php?
    for(i=1;1<=3;i++):

    //My code

endfor:

?>

I used as above code.

No correct solution

OTHER TIPS

**enter image description here**

  <span>How to use for loop in PHTML page</span>
<?php
for ($i = 0; $i <= 5; $i++) {
  echo "The number is: $i <br>";
}
?>

control-structures.for.php

instead of for loop, You can use foreach like:

<?php 

$variable = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6 );

$count = 1;
foreach ($variable as $key => $value) {
    if($count >5){continue;}
        echo $value;
    $count++;
}

Advantage: in foreach loop you do need to check condition and increment operator. it will loop automatic for all elements

Try this way:

If you need to print custom html inside the cycle:

<?php for ($i = 1; $i <= 5; $i++): ?>
    <h1>test</h1>
<?php endfor; ?>

Otherwise:

<?php
    for ($i = 1; $i <= 5; $i++) {
        echo $i;
    }
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top