Re: Problem with a while loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux