You are on page 1of 23
arenes casting - How do comrta sng ta pumbor in PHP? Stack Oerlow How do | convert a string to a number in PHP? Asked 9 years, 3months ago Active 6 months ago Viewed 2.0m times. 734 Iwant to convert these types of values, °3°, *2.34', '@.234343" , ett. to a number. In JavaScript we can use Nunber() , butis there any similar method available in PHP? Input output a 2 "2.34" 2.34 "@.3454asa5° 0.345455 php casting —type-conversion ‘Share Improve this question Follow edited Jun 24°19 at 19:42 asked Dec 16 11 at 4:08 YanDatsiuk Sara 1295 2 13 27 9k 12 32 49 79 Reader beware: there is no real answer to this question :( — Matthieu Napoli Jul 8 13 at 16:05 @MatthieuNapoli The answer is that usually Php figures it out for you - one of the perks of a dynamic type system, — Kolob Canyon May 26 16 at 18:40 12 With all the chains of uncertainty and ‘usually’. - person27 Feb 6 '17 at 17:48, | think what | meant 5 years ago is that there is not a single function that takes the string and retums a proper int or float (you usually dont want a float when an int is given). — Matthieu Napoli Apr 3018 at 6:48 7 4 @MatthieuNapoli | am glad you clarified your point to mean there is more than one way to skin a cat, rather than there is no way to do this. Casting is very important in database operations, for instance. For ‘example on a parameterized PDO query, there will be a struggle sometimes for the parser to realize it is a number and not a string, and then you end up with a0 in an integer field because you did not cast the string to an int in the parameter step. ~ slubsthewizard Sep 2018 at 16:19 hpevistacoverflow.camquestions/8529650honint or parseFloat() to convert string=>float. but in general javaScript use Number() to convert string => number. Iwant a similar method in php? - Sara Dec 16 '11 at 4:25 ' It was useful for me when | had a month number obtained from a full date string, wich I used to pull a value from a moth names array. Auto casting doesn't happen here because string indexes are valid, but not equivalent. — Cazuma Nii Cavalcanti Jan 30 13 at 20:39 24 Depending on the context, it might not be safe to assume that . is the decimal point separator. en.wikipedia ora/wikl... ~ Dave Janis Feb 23 ‘14 at 3:21 / 1 @Sara: You can also create a function for converting the string to number by first casting it to integer, then to float and then comparing if both values (integer and float) are equal. if they are, you should retum the value as integer, ifnot, then as a float. Hope you get the idea. ~ Youstay Igo Aug 416 at 4:38 3 ‘There are times that you need to pass the integer to file system, it is not a good idea to wait for PHP to do the conversion for you. — AaA Jan 27 19 at 14:49 hpevistacoverflow.camquestions/8529650hon (int) 10; "10.1" > (float) 10.1; — Holly Aug 2017 at 8:43 hpevistacoverflow.camquestions/8529650hon, as it has different meanings for numbers and strings (i.e, "10" < "2" but 10 > 2). — rivJul 2915 at 14:15 27 if you want get a float for $value = ‘9.4' , but intfor $value = ‘4* , you can write: $number = ($value == (int) $value) ? (int) $value : (float) $value; Itis little bit dirty, but it works. Share Improve this answer Follow edited Feb 2519 at 19:18 answered Sep 15°14 at 4:44 Peter Mortensen Aleksei Akireikin 278k 21 94 123 1909 14 21 hpevistacoverflow.camquestions/8529650hon it should convert to 5 and if its "2.345" should convert to 2.345. - Sara Dec 16'I1 at 4:15 12 Just a litle note to the answers that can be useful and safer in some cases. You may want to check if the string actually contains a valid numeric value first and only then convert it to a numeric type (for example if you have to manipulate data coming from a db that converts ints to strings). You can use is_numeric() andthen floatval() : $a = “whatever”; // any variable if (is_numeric($a)) var_dump(floatval($a)); // type is float else var_dump($a); // any type Share Improve this answer Follow answered Aug 1013 at 17:16 taseenb 1201 1 14 27 hpevistacoverflow.camquestions/8529650hon Source: https :/www.php.net/manual/en/function is-numeric.php#107326 Share Improve this answer Follow answered Sep 2920 at 8:11 klodoma, 3329 1 22 32 6 hpevistacoverflow.camquestions/8529650hon= 1¢0000 evaluates to false, but "19e0000million” >= 1900000 evaluates to true See also: https://www.php,net/manual/en/lanquage.operators.comparison.php how conversions are done while comparing https:/www.php.net/manual/en/language types. string.php#ianguage.types.string.conversion how strings are converted to respective numbers ‘Share Improve this answer Follow edited Jun 15'19 at 17:57 answered Jun 1519 at 17:52 Dragas 5484 19 This is a very good answer. I worked up a bit and wrote some tests for the same Lules.in/php-caveats.int. float-type-conversion ~ th3pirat3 Jul 4 '20 at 13:10 is_numeric($str) helps with this. (As shown in two of the earlier answers - so its not quite accurate that "nobody mentioned the biggest caveat” - though I see no one explained the issue in detail.) — ToolmakerSteve Aug 13 '20 at 23:00 #* hpevetacoverlowcamquestions/8529656ton-o--corver-a-sting-o-a-number php 1628 arenes casting - How do comrta sng ta pumbor in PHP? Stack Oerlow You can use: (int) $var) (but in big number it return 2147483647 :-) ) But the best solution is to use: if (is_numeric($var)) Svar = (isset($var)) ? $var : @ else Svar = ©) Or if (is_numeric($var)) Svar = (trim($var) ") 20: Svar; else Svar = 0; Share Improve this answer Follow edited Apr 13°14 at 8:26 answered Sep 2913 at 19:18 Peter Mortensen Mehdi Ajdari 278k 21 94 128 m1 8 3 ‘Simply you can write like this: cast_to_number("123.45"); 11 (float) 123.45 cast_to_number('-123.45"); 1 (Float) -123.45 cast_to_number("123"); 11 Gint) 123 cast_to_number(*-123"); 1H Gant) 123 cast_to_number("foo 123 bar’); // false Share Improve this answer Follow answered Sep 1020 at 10:46 Benni 837 7 13 4 PHP will do it for you within limits

You might also like