Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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)"; }
echo ${"title_" . $i};
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; }
Add a comment
This should work, haven't tested
for($i=1;$i<=$number;$i++){ echo ${"title_$i"}; }
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.
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
echo ${"title_" . $i};