0
I have a variable $abas = 4
.
I need PHP to make one echo
in the variable number, in this case it would be 4.
The result should be:
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
I am a beginner in PHP, what is the best way to do this?
0
I have a variable $abas = 4
.
I need PHP to make one echo
in the variable number, in this case it would be 4.
The result should be:
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
I am a beginner in PHP, what is the best way to do this?
7
For what you are wanting to do, I recommend using the for
.
Example:
<?php for ($i = 1; $i <= $abas; $i++): ?>
<li><?= $i ?></li>
<?php endfor ?>
The for
is a Repetition Structure, which, as the name suggests, is intended to perform a number of repetitions while the second parameter condition is met.
It is divided into three parts:
$i = 1
is the initial definition value.$i
is less than or equal to $abas
. $i++
increment +1
in $i
for the next loop.The excerpt <?= $i ?>
will be in charge of printing the current value of $i
in HTML.
In versions prior to 5.4 of PHP, you would have to use <?php echo $i; ?>
.
Browser other questions tagged php for
You are not signed in. Login or sign up in order to post.
show, that’s right
– Leandro Marzullo