Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert 01, 02, 03, 04 .. 09 to 1,2,3,4 ... 9

Tags:

php

int

Hay how can I convert

01 to 1
02 to 2

all the way to 9?

Thanks

like image 835
dotty Avatar asked Nov 27 '22 23:11

dotty


2 Answers

I assume the input is a string?

$str = "01";
$anInt = intval($str);

You may think that the leading 0 would mean this is interpreted as octal, as in many other languages/APIs. However the second argument to intval is a base. The default value for this is 10. This means 09->9. See the first comment at the intval page, which states that the base deduction you might expect only happens if you pass 0 in as the base.

like image 118
Doug T. Avatar answered Dec 05 '22 01:12

Doug T.


$x="01";
$x=+$x;

$x="02";
$x=+$x;

...

or

$x=+"01";

should work for both int, and string

like image 39
YOU Avatar answered Dec 05 '22 02:12

YOU