Page 1 of 1

Strange Error

Posted: Sun Feb 25, 2007 8:26 am
by nwp
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

Posted: Sun Feb 25, 2007 8:44 am
by louie35
can you not use

Code: Select all

str_replace
function

Posted: Sun Feb 25, 2007 8:54 am
by nwp
louie35 wrote:can you not use

Code: Select all

str_replace
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 ??

Posted: Sun Feb 25, 2007 9:08 am
by anjanesh
Anything integer starting with the digit 0 is represented as an octal.

42 in decimal is 52 in octal

Posted: Sun Feb 25, 2007 9:28 am
by nwp
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

Posted: Sun Feb 25, 2007 9:29 am
by Chris Corbyn

Code: Select all

echo ltrim("052", "0");

Posted: Sun Feb 25, 2007 9:36 am
by nwp
d11wtq wrote:

Code: Select all

echo ltrim("052", "0");
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

Posted: Sun Feb 25, 2007 10:32 am
by feyd
What's the problem with using 52? Without being a string, a leading zero is octal notation. There's no way around that.

Posted: Sun Feb 25, 2007 10:45 am
by anjanesh
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.

Posted: Sun Feb 25, 2007 11:19 am
by Chris Corbyn
~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); 
?>

Posted: Mon Feb 26, 2007 12:23 am
by neel_basu
Would you tell us why you are trying to doing this e.g. sending with preceding zero

Posted: Mon Feb 26, 2007 12:26 am
by nwp
i was making a function to change a number to words and then i faced this problem

Posted: Mon Feb 26, 2007 12:37 am
by neel_basu
To do this job you can use this function
viewtopic.php?p=361151#361151