coder9 Posted July 17, 2008 Share Posted July 17, 2008 Ok guys I have this codes below which has the variable name '$tmpbackcolor1' from 1 to 100, which is very annoying because it's too long. $tmpbakcolor1="#FF0000"; $tmpbakcolor2="#FF0000"; $tmpbakcolor3="#FF0000"; $tmpbakcolor4="#FF0000"; $tmpbakcolor5="#FF0000"; ... $tmpbakcolor99="#FF0000"; $tmpbakcolor100="#FF0000"; Now how do i convert it into array variables and just loop it to make it shorter. this easy for coder who is very familiar with array and some loop functions. Your help is a big enhancement to my codes. therefore thank you in advance. Link to comment https://forums.phpfreaks.com/topic/115189-solved-how-to-make-this-code-shorter-using-looping-the-array/ Share on other sites More sharing options...
unkwntech Posted July 17, 2008 Share Posted July 17, 2008 I would make $tmpbakcolor into an array, then use a while loop to step through and assign each variable. $tmpbakcolor = array(); $i=1; while($i<100) { $tmpbakcolor[$i] = "#FF0000"; $i++; } Link to comment https://forums.phpfreaks.com/topic/115189-solved-how-to-make-this-code-shorter-using-looping-the-array/#findComment-592299 Share on other sites More sharing options...
coder9 Posted July 17, 2008 Author Share Posted July 17, 2008 I would make $tmpbakcolor into an array, then use a while loop to step through and assign each variable. $tmpbakcolor = array(); $i=1; while($i<100) { $tmpbakcolor[$i] = "#FF0000"; $i++; } Thanks unkwntech, your da best I LOVE YOU! Link to comment https://forums.phpfreaks.com/topic/115189-solved-how-to-make-this-code-shorter-using-looping-the-array/#findComment-592341 Share on other sites More sharing options...
kenrbnsn Posted July 17, 2008 Share Posted July 17, 2008 Here's an even shorter solution using the array_fill() function: <?php $tmbakcolor = array_fill(1,100,'#FF0000'); ?> Ken Link to comment https://forums.phpfreaks.com/topic/115189-solved-how-to-make-this-code-shorter-using-looping-the-array/#findComment-592351 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.