...

View Full Version : How can I make a set of different strings using a loop? is it possible?



maltrecho
12-04-2003, 09:52 AM
The following is just an example, but is exactly what I need to know to apply it to the real code. How can I do it?



/*THIS NUMBER WILL BE DEFFERENT EACH TIME */

$number = 5;

for ($i = 1; $i <= $number; $i++) {

/* THIS DOESNT WORK */

$string_$i = ($i + 5);

}

So the result should be:


$string_1 = 6;
$string_2 = 7;
$string_3 = 8;
$string_4 = 9;
$string_5 = 10;

firepages
12-04-2003, 03:00 PM
arrays may be easier in the long run ... but anyway , look for 'variable variables' section of the manual...



<?php
$number = 5;

for ( $i = 1 ; $i <= $number ; ++$i ) {
${'string_'.$i} = $i + $number ;
}

echo $string_5;
?>

maltrecho
12-06-2003, 07:25 PM
Thanks firepages, after a long fight it finally worked! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum