PHP Loop with Counts

I'm needing help with logic on my PHP loop.

So I have user input that comes in the form of a number anything between 18 and 100.  They are $agemin and $agemax.  Based on their values I dynamically create bit of code.  I need some help with the logic.  Below is an example.

$agemin = 18;
$agemax = 24;

Not sure which, but need a php loop here I assume

$population .= ' + age'.$ageCount;

Open in new window


My output of $population based on the above example would be:

+ age18 + age19 + age20 + age21 + age22 + age23 + age24
LVL 12
Nathan RileyFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
This should do what you want.
$population = '';
for($i=$agemin; $i<=$agemax;$i++) {
  $population .= " + age{$i}";
}
echo $population

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
PHP has the range() function that is useful for things like this.  No loops needed!  You might trim() the data, depending on how you intend to use this string.
http://iconoun.com/demo/temp_nathan_riley.php
Outputs: string(56) " + age18 + age19 + age20 + age21 + age22 + age23 + age24"

<?php // demo/temp_nathan_riley.php
/**
 * http://www.experts-exchange.com/questions/28691479/PHP-Loop-with-Counts.html
 */
error_reporting(E_ALL);

$txt = ' + age';
$str = $txt . implode($txt, range(18,24));
var_dump($str);

Open in new window

0
Nathan RileyFounderAuthor Commented:
Ah, didn't see your message before I accepted, sorry ray.
0
Ray PaseurCommented:
NP, there are often many ways to get to the same solution.  Best regards, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.