OK, stupid question.. but its killing me!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

OK, stupid question.. but its killing me!

Post 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!
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Unless you do $i = '032'; then it will treat $- = 032; as an octal number (0 meaning octal :o);

So you can do:

Code: Select all

$i = '032';
$i++;
printf('%03d', $i);
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

do you mind explaining what the %03d does??
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

muchas gracias!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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 ;)
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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!!
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post 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!
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

I'm an idiot...

added this line and it al works fine... Thanks again!

Code: Select all

$photonumber = sprintf('%03d', $photonumber);
Post Reply