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
MedaXis
Forum Newbie
Posts: 16 Joined: Tue Nov 12, 2002 12:49 pm
Location: the netherlands
Post
by MedaXis » Tue Jan 21, 2003 8:27 am
Hi all,
I have this variable which contains a number:
I want to decrease it by 1. I tried to do this:
Code: Select all
$temp = $the_number - 1;
print $temp;
It did not print 0009 (the number I want) but it printed 7
So I have two questions:
1- why does (0010 - 1) equal 7 ?
2- How do I solve my initial problem (how to decrease a 4 digit number by 1)?
Steve
puckeye
Forum Contributor
Posts: 105 Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:
Post
by puckeye » Tue Jan 21, 2003 8:59 am
Hi Steve,
The fact that you wrote $the_number = 0010 tells PHP that you are dealing with an octal (base 8) number so (0010 in octal == 8 in decimal).
Simply write $the_number = intval(0010); and it'll work OK.
Also to decrement you can use the following:
EDIT 1
To print out using your 4 digits format you'll have to format the output.
Check this out :
http://www.php.net/manual/en/function.sprintf.php I'll let you figure out how to format your output.
Last edited by
puckeye on Tue Jan 21, 2003 1:32 pm, edited 2 times in total.
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Jan 21, 2003 9:06 am
It's because by putting a zero in front of the number you are telling PHP you want to treat it as an octal:
http://www.php.net/manual/en/language.types.integer.php
If you do:
Code: Select all
$num = '0010';
$num_subone = $num - 1;
echo $num_subone;
Then you get the expected result - 9.
You can then format that number to pad it out with leading zero's by using
printf() :
Edit: oops a bit slow posting this but the printf() info might be useful
Mac
MedaXis
Forum Newbie
Posts: 16 Joined: Tue Nov 12, 2002 12:49 pm
Location: the netherlands
Post
by MedaXis » Tue Jan 21, 2003 10:25 am
so what do I do if I want to store 0009 in a variable?
I tried puckeye's method but for some reason it still resulted 7
Steve
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Jan 21, 2003 10:28 am
Put it into a string:
PHP is loosely typed so it will happily do maths on strings.
Mac
MedaXis
Forum Newbie
Posts: 16 Joined: Tue Nov 12, 2002 12:49 pm
Location: the netherlands
Post
by MedaXis » Tue Jan 21, 2003 11:24 am
maybe I am missing the point here but I still need it to be a 4 digit number
Steve
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Jan 21, 2003 12:54 pm
It still will be, although it will be typed as a string PHP won't consider it an octal and will do normal base 10 maths on it. Try the following code:
Code: Select all
$num = '0010';
$num_subone = $num - 1;
printf('%04d', $num_subone);
Mac
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Jan 21, 2003 12:59 pm
on the other hand if you're using printf with a width-flag there's no need to define the
number with 4-digit-width at all
Code: Select all
$num = 10;
printf('%04d', $num - 1);
puckeye
Forum Contributor
Posts: 105 Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:
Post
by puckeye » Tue Jan 21, 2003 1:34 pm
MedaXis wrote: so what do I do if I want to store 0009 in a variable?
I tried puckeye's method but for some reason it still resulted 7
Steve
Sorry I should have writen:
Code: Select all
$the_number = intval("0010"); // using quotes around the number
MedaXis
Forum Newbie
Posts: 16 Joined: Tue Nov 12, 2002 12:49 pm
Location: the netherlands
Post
by MedaXis » Wed Jan 22, 2003 3:20 am
allright I get it now, thanks a lot!
Steve