...

View Full Version : Convert number as string to number?



anotherJEK
04-16-2012, 09:07 PM
I thought php had parseInt or something of the sort (as in javascript).
......
I just figured out how to use settype....

false alarm

Fou-Lu
04-16-2012, 09:24 PM
PHP automatically converts based on context. So adding two strings of numbers results in a number. This is IMO a tremendous weakness of using datatype weak languages.
For explicit, I'd cast directly using (int) or (float) if you need to. Settype is a tad on the temperamental side. Even using an intval() would be better.

felgall
04-16-2012, 10:48 PM
I thought php had parseInt or something of the sort (as in javascript).

The PHP equivalent to JavaScripts parseInt() is base_convert() which is even more flexible since parseInt can only convert to base 10 from bases between 2 and 36 whereas base_convert can convert between any two number bases.

So JavaScript:


$num = parseInt('f',16);

PHP equivalent


$num = base_convert('f', 16, 10);

Neither has anything to do with converting strings to numbers.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum