I am trying to convert 08 and 09 to 8 and 9 respectively, but they are not getting converted.
I tried 08+0 , 09+0 but the result I obtained in both the cases is 0.
The above method is working for 01 to 07 and I am obtaining results as 1,2,...7, but not for 08 and 09. Please help me out.

Recommended Answers

All 6 Replies

If you precede an integer with a zero, it is assumed to be in octal notation. If you need an integer outputted with a leading zero, use sprintf().

cast it to integer:

$var = "09";
echo (int)$var;

or

number_format($var)

i also used (int)$var and number_format($var),sprintf but it still returning 0 for o8;

wait wait u said 08 not o8, its different. is it zero or leter O in front of the 8?

turn the string array by using explode function in this manner $var2array=explode("",$var); then use print_r($var2array)

it should have array with these value [0]=>'0', [1]=>'8' now to remove the 0 then you'll have to perform looping.

$var2array_2=array();
if($var2array[0]!=0){
echo $var=implode("",$var2array);
}//automatic ends if  block if first index is not 0
else{
for($i=0;$i<strlen($var);$i++)
if($var2array[0]==0){
$var2array[0]='';
}//very easy... because it just only first index to check
//what if there are more than 1 zero's
if($i>0)&&($var2array[$i-1]!=0){
array_push($var2array_2,$var2array[$i]);
}

}
echo implode("",$var2array_2);

in this code

if there is an input 0808 it shouldbe 808
if there's an input 008080 it shoulbe 8080

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.