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
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Sun Feb 25, 2007 8:26 am
I am getting this Strange Error
Code: Select all
<?php
function hundreed($num_str)
{
$to_str = (string)($num_str);
echo $num_str."\t".$to_str."\n";
}
?>
<?php
$num = 052;
echo "\n".hundreed($num);
?>
This Is Making Output
Browser wrote: 42 42
Not 52 52
Why ??
Even
Code: Select all
<?php
function hundreed($num_str)
{
echo $num_str;
}
$num = 052;
hundreed($num);
?>is also making output
Browser wrote: 42
not 52
louie35
Forum Contributor
Posts: 144 Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:
Post
by louie35 » Sun Feb 25, 2007 8:44 am
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Sun Feb 25, 2007 8:54 am
louie35 wrote: can you not use
function
Sorry I only need to know why I am getting this strange Error . Is it a Bug ??. Are all others getting this same ERROR ??
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Sun Feb 25, 2007 9:08 am
Anything integer starting with the digit 0 is represented as an
octal .
42 in decimal is 52 in octal
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Sun Feb 25, 2007 9:28 am
So how can I get both 052 and 52 treated as Decimal.
octdec() will treat all also 52 as a octal and chang It is there any function like trim() for Integer
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Sun Feb 25, 2007 9:36 am
But it treats 052 and 0 as Strings not as integerSo
Code: Select all
<?php
$salt = 052;
$key = 0;
echo ltrim($salt, $key);
?>
So this also outputs 42
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Feb 25, 2007 10:32 am
What's the problem with using 52? Without being a string, a leading zero is octal notation. There's no way around that.
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Sun Feb 25, 2007 10:45 am
I think he is getting $num from another source, but in most cases it should be able to be streamed in as a string instead of integer 052.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sun Feb 25, 2007 11:19 am
~nwp, the function synopsis for ltrim() uses strings so it will type cast. You can reverse that by type-casting ti back again:
Code: Select all
<?php
$salt = 052;
$key = 0;
echo (int) ltrim($salt, $key);
?>
neel_basu
Forum Contributor
Posts: 454 Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India
Post
by neel_basu » Mon Feb 26, 2007 12:23 am
Would you tell us why you are trying to doing this e.g. sending with preceding zero
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Mon Feb 26, 2007 12:26 am
i was making a function to change a number to words and then i faced this problem