1

I hvae three variables,

$title_1 $title_2 $title_3

how can i print them in for loop?

what i've tried:

$number = 3;
    for($i=0;$i<$number;$i++){
            echo "$title_($i+1)";
    }
2
  • Try echo ${"title_" . $i}; Commented Dec 17, 2012 at 10:31
  • 4
    Use an array. Anyone who suggests anything else is doing it wrong. Commented Dec 17, 2012 at 10:32

3 Answers 3

5

Don't meddle with these things; use an array for this stuff.

$titles = array('first title', 'second title', 'third title');

foreach ($titles as $title) {
    echo $title;
}
Sign up to request clarification or add additional context in comments.

Comments

5

This should work, haven't tested

for($i=1;$i<=$number;$i++){
        echo ${"title_$i"};
}

Comments

4

You can do this:

for($i = 0; $i < $number; $i++) {
    $var = "title_".$i;
    echo $$var;
}

But I wouldn't. This is really, REALLY bad design. Use arrays.

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.