something about 08 , 09 and function

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
fird01
Forum Newbie
Posts: 19
Joined: Mon Apr 07, 2008 11:04 pm

something about 08 , 09 and function

Post by fird01 »

Code: Select all

 
$a=testme(08,09);
echo $a;
 
function testme($a,$b){
$c=$a+$b;
return $c;
}
 
theres something about 08, 09 and function because that code will output 0..
but if u change the input to 9 and 8 or 01 to 07 instead of 09 and 08 the correct out put will be produce..
so what am i doing wrong here.. is it my apache or ??? and why does function convert number from 01 to 1 or 03 to 3 . how do i maintain the 01 and 02 when passing the data to function.. cause im doing a query with those value.. so 1 is different from 01 when you do a select statement..
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: something about 08 , 09 and function

Post by Mordred »

0x10 = 16 (hexadecimal)
010 = 8 (octal)
07 = 7 (octal)
08 = 0 (invalid octal)
fird01
Forum Newbie
Posts: 19
Joined: Mon Apr 07, 2008 11:04 pm

Re: something about 08 , 09 and function

Post by fird01 »

Mordred wrote:0x10 = 16 (hexadecimal)
010 = 8 (octal)
07 = 7 (octal)
08 = 0 (invalid octal)
ok.. so now php define that any number that start with 0 is octal.. so how does i solve the problem my not making it think it is an octal.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: something about 08 , 09 and function

Post by Kieran Huggins »

You can force it to see decimal by using the intval() function with the second argument.

Note that the base argument only applies to strings, so you'll have to convert your integer to a string and then convert back to integer with the appropriate base, like this:

Code: Select all

$a=testme(08,09);
echo $a;
 
function testme($a,$b){
  $c = intval(strval($a),10) + intval(strval($b),10);
  return $c;
}
OR

You could get in the habit of quoting your integers. They'll either arrive as strings or be represented without the leading zeroes anyway, just not in the example you gave.
fird01
Forum Newbie
Posts: 19
Joined: Mon Apr 07, 2008 11:04 pm

Re: something about 08 , 09 and function

Post by fird01 »

o..ok thx.. quoting seems the easier thing to do..
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: something about 08 , 09 and function

Post by Kieran Huggins »

I totally agree.
Post Reply