1

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?

2
  • 1
    Would be a whole lot easier if you had $textq = array('text','more text'); rather than $textq1 = 'text'; $textq2 = 'more text'; Commented Mar 1, 2012 at 17:11
  • @MarkBaker - yep, sure would be easier in an array but I've inherited a whole host of code... Commented Mar 1, 2012 at 18:45

4 Answers 4

3

Change:

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

To:

echo "<div>". ${'textq'.$i} ." ";
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

That would result in Parse error: syntax error, unexpected T_VARIABLE error.
Yes it would. Would work if it were echo "<div>{${"textq$i"}}</div>"; (need quotes around the variable name).
1
$var = "textq{$i}";
echo "<div>". $$var." ";

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.