How to use for loop in PHTML page
-
14-04-2021 - |
Pergunta
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.
Nenhuma solução correta
Outras dicas
<span>How to use for loop in PHTML page</span>
<?php
for ($i = 0; $i <= 5; $i++) {
echo "The number is: $i <br>";
}
?>
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;
}
?>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange