Page 1 of 1
OK, stupid question.. but its killing me!
Posted: Sun Apr 04, 2004 8:15 pm
by hurdy gurdy
I want to increment a value by one, while retaining the amount of characters in that value. For instance:
Code: Select all
$i = 032;
$i++;
echo $i; // returns 33, not 033
How do I keep the format of the number consistent, meaning that the integer remains three characters regardless if the value is less than a hundred?
Did that question make any sense? I must be tired...
Thanks for any help on this!
Posted: Sun Apr 04, 2004 8:21 pm
by Illusionist
woh! Does anyone know why the exact same code ran on my server returns 27??
Code: Select all
$i = 032;
$i++;
echo $i; //returns 27 not 33
Posted: Sun Apr 04, 2004 8:26 pm
by markl999
Unless you do $i = '032'; then it will treat $- = 032; as an octal number (0 meaning octal

);
So you can do:
Code: Select all
$i = '032';
$i++;
printf('%03d', $i);
Posted: Sun Apr 04, 2004 8:30 pm
by Illusionist
do you mind explaining what the %03d does??
Posted: Sun Apr 04, 2004 8:33 pm
by hurdy gurdy
muchas gracias!
Posted: Sun Apr 04, 2004 8:33 pm
by markl999
%03d basically means pad the number with 3 leading 0's (the d means you'll be padding an integer value)
The
sprintf docs has details of all the various arguments you can use

Posted: Sun Apr 04, 2004 8:40 pm
by Illusionist
ahh! thanks a lot mark! i was looking at [php_man]printf()[/php_man] but wasn't finding anything. I didn't even think to look at the related functions! Thansk!!
Posted: Sun Apr 04, 2004 8:45 pm
by hurdy gurdy
Sorry Guys, this just doesn't seem to work for me... maybe I should be more specific. Heres the code I'm working with:
Code: Select all
<?php
$photonumber = 006; // an example of a value it could have
$filecount = 110; // another example of a value
if ($photonumber <= 000)
{
$photonumber = $filecount;
}
$photonumber = ($photonumber - 1);
echo '<a href="galleryImage.php?date='.$date.'&modelIDnumber='.$modelIDnumber.'&photonumber='.$photonumber.'&filecount='.$filecount.'">previous</a>';
?>
The value of $photonumber becomes 5, not 005.
See example of the url it passes:
http://www.adomainnamehere.com/galleryI ... ecount=110
How do I retain that three digit number?
gracias again!
Posted: Sun Apr 04, 2004 8:51 pm
by hurdy gurdy
I'm an idiot...
added this line and it al works fine... Thanks again!
Code: Select all
$photonumber = sprintf('%03d', $photonumber);