Re: Problem with a while loop

From: Date: Thu, 07 Oct 2004 20:27:49 +0000
Subject: Re: Problem with a while loop
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-198894@lists.php.net to get a copy of this message
Richard Kurth wrote:
I am having a problem with a while loop if I put in a number with 0's in front of it it converts the number two the first whole number. such as 00001 would be 1 02200 would be 2200. Then it starts the count down starting with that number. How can I make it not remove the 0's in front of the number these are needed to search a certain database . The following script would look like this when run 00001 2 3 4 I needed it to look like this 00001 00002 00003 <?php $startnumber="00001"; $stopnumber="02200"; $i = $startnumber; echo $i; while ($i <= $stopnumber): echo "<br>" ; echo $i; ++$i; endwhile; ?>
Here's the problem. You set the number originally as a string. However, as soon as you tell PHP to add the number, it becomes an integer. It must. You can't add strings. If you need to use the number as a string inside the loop, you will have to convert it back to a string. You can do this with sprintf, as in: $str_num = sprintf("%05.0f",$i); You can use var_dump($varname) to see what kind of data is stored in $varname. Janet -- Janet Valade -- janet.valade.com

« previous php.general (#198894) next »