Homer_J
Homer_J

Reputation: 3323

PHP variable in loop

I have the following:

$textq1 = 'text';
$textq2 = 'more text';

I have a loop:

for ($i = 1; $i <= 70; $i++) {

    echo "<div>". $textq1." ";

}

Now....what I'd like to do is, within the loop have the 1 part of the $textq1 be the value of $i.

Is that possible?

Upvotes: 1

Views: 116

Answers (4)

Sarfraz
Sarfraz

Reputation: 382909

Change:

echo "<div>". $textq1." ";

To:

echo "<div>". ${'textq'.$i} ." ";

Upvotes: 3

Travesty3
Travesty3

Reputation: 14489

$var = "textq{$i}";
echo "<div>". $$var." ";

Upvotes: 1

Andy
Andy

Reputation: 50640

Take a look at this example about variable variables in the PHP docs: http://www.php.net/manual/en/language.variables.variable.php#105282

Upvotes: 0

csanyigabor
csanyigabor

Reputation: 126

for (...)
{
    echo "<div>{${textq$i}}</div>";
}

Upvotes: 1

Related Questions