Page 1 of 1

something about 08 , 09 and function

Posted: Thu Jun 19, 2008 12:25 am
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..

Re: something about 08 , 09 and function

Posted: Thu Jun 19, 2008 12:42 am
by Mordred
0x10 = 16 (hexadecimal)
010 = 8 (octal)
07 = 7 (octal)
08 = 0 (invalid octal)

Re: something about 08 , 09 and function

Posted: Thu Jun 19, 2008 1:22 am
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.

Re: something about 08 , 09 and function

Posted: Thu Jun 19, 2008 2:21 am
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.

Re: something about 08 , 09 and function

Posted: Fri Jun 20, 2008 4:52 am
by fird01
o..ok thx.. quoting seems the easier thing to do..

Re: something about 08 , 09 and function

Posted: Fri Jun 20, 2008 5:35 am
by Kieran Huggins
I totally agree.