0

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.

0

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

| improve this answer | |
  • I need to run the loop 5 time only how can i specify it here – Sabareesh May 27 at 8:35
  • I need the condition checking and want to loop – Sabareesh May 27 at 8:37
  • You can add a count variable and check condition, please see my update answer. – Pawan May 27 at 8:45
0

**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

| improve this answer | |
0

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;
    }
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.