chch
chch

Reputation: 35

How to use variable to get the for loop's value in PHP?

How to use a varible to store the value of the for loop?

for($i=1;$i<=$var;$i++)

How to get $i's value and store into another value and show its result?

I am a beginner of PHP and I want to improve my concept, I am very grateful if anyone can helps, Cheers!

Upvotes: 0

Views: 64

Answers (2)

sazedul
sazedul

Reputation: 46

Are you want dynamic variable or just a normal variable which will give you the out put like
12345678910

<?php 
$var=10;
$a='';
for($i=1;$i<=$var;$i++){
$a.=$i;
}
echo $a;
?>

output: http://3v4l.org/2p8L6

or if you want dynamic variable then use following code to make a variable dynamic

${"num_" . $i} = $i;

output will be

$num_1=1; 
$num_2=2;
$num_3=3; 

etc

Upvotes: 2

Thomas Edison
Thomas Edison

Reputation: 96

In plain English; PHP is updating the value stored in lopping variable automatically, and this is the whole idea behind using for loops, not only in PHP, but also in other programming languages.

Usually, you would want to use it (the variable $i) as a kind of a counter, and run code in the loop based on the iteration count.

Upvotes: 0

Related Questions