Link to home
Start Free TrialLog in
Avatar of Nathan Riley
Nathan RileyFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of Nathan Riley

ASKER

Ah, didn't see your message before I accepted, sorry ray.
NP, there are often many ways to get to the same solution.  Best regards, ~Ray