Strange Error

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
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Strange Error

Post 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
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

can you not use

Code: Select all

str_replace
function
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post 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 ??
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

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 »

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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

echo ltrim("052", "0");
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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); 
?>
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

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 »

i was making a function to change a number to words and then i faced this problem
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

To do this job you can use this function
viewtopic.php?p=361151#361151
Post Reply